Skip to content

Commit 1e5642b

Browse files
authored
ci: add test github workflows
* 👷 ci: add test github workflows * ➕ chore: add temp build file * 🐛 fix: fix user failOnWarnings param always be true * 🐛 fix: fix warning about require from webpack * ✅ test: add multiply base test case
2 parents 64bb7c3 + a3555dd commit 1e5642b

10 files changed

Lines changed: 154 additions & 5 deletions
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Test Multiply Base
2+
3+
on:
4+
push:
5+
branches:
6+
- test-github-action
7+
8+
jobs:
9+
test-action-works:
10+
runs-on: ubuntu-latest
11+
name: test no config file
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
16+
- name: test multiply base
17+
uses: ./
18+
with:
19+
files: './examples/no-config-file ./examples/only-warning'
20+
failOnWarnings: true
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Test Js Config
2+
3+
on:
4+
push:
5+
branches:
6+
- test-github-action
7+
8+
jobs:
9+
test-action-works:
10+
runs-on: ubuntu-latest
11+
name: test js config
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
16+
- name: test js config
17+
uses: ./
18+
with:
19+
files: './examples/js-config'
20+
failOnWarnings: true
21+
configFile: './examples/js-config/.lintmdrc.js'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Test No Config File
2+
3+
on:
4+
push:
5+
branches:
6+
- test-github-action
7+
8+
jobs:
9+
test-action-works:
10+
runs-on: ubuntu-latest
11+
name: test no config file
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
16+
- name: test no config file
17+
uses: ./
18+
with:
19+
files: './examples/no-config-file'
20+
failOnWarnings: true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Test Only Warning
2+
3+
on:
4+
push:
5+
branches:
6+
- test-github-action
7+
jobs:
8+
test-action-works:
9+
runs-on: ubuntu-latest
10+
name: test only warning
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
15+
- name: test only warning
16+
uses: ./
17+
with:
18+
files: './examples/only-warning'
19+
configFile: './examples/only-warning/.lintmdrc'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Test Use Config File
2+
3+
on:
4+
push:
5+
branches:
6+
- test-github-action
7+
jobs:
8+
test-action-works:
9+
runs-on: ubuntu-latest
10+
name: test use config file
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
15+
- name: test use config file
16+
uses: ./
17+
with:
18+
files: './examples/use-config-file'
19+
failOnWarnings: true
20+
configFile: './examples/use-config-file/.lintmdrc'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Test Use Custom Config
2+
3+
on:
4+
push:
5+
branches:
6+
- test-github-action
7+
8+
jobs:
9+
test-action-works:
10+
runs-on: ubuntu-latest
11+
name: test use custom config
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
- name: test use custom config
16+
uses: ./
17+
with:
18+
files: './examples/use-custom-config'
19+
failOnWarnings: true
20+
configFile: './examples/use-custom-config/hello'

.github/workflows/test.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Unit Test
2+
3+
on: [ pull_request ]
4+
5+
jobs:
6+
unit-test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
11+
- name: Install modules
12+
run: yarn install
13+
14+
- name: Run tests
15+
run: yarn test

__tests__/github-action.spec.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ describe('lint-md GitHub action 测试', () => {
7575
expect(lintMdAction.isPass()).toStrictEqual(true)
7676
})
7777

78-
test('用户在 GitHub Action 将 failOnWarnings 值设为 true,本次 ci fail', async () => {
78+
test('用户在 GitHub Action 将 failOnWarnings 值设为 true,即使只有 warning 本次 ci fail', async () => {
7979
process.env.GITHUB_WORKSPACE = path.resolve(process.cwd(), 'examples', 'only-warning')
80-
mockAction(null, '.lintmdrc', true)
80+
mockAction(null, '.lintmdrc', 'true')
8181
const lintMdAction = new LintMdAction()
8282
// lint
8383
await lintMdAction.lint()
@@ -86,10 +86,22 @@ describe('lint-md GitHub action 测试', () => {
8686

8787
test('用户自定义的配置文件为 JavaScript 模块', async () => {
8888
process.env.GITHUB_WORKSPACE = path.resolve(process.cwd(), 'examples', 'js-config')
89-
mockAction(null, '.lintmdrc.js', true)
89+
mockAction(null, '.lintmdrc.js', 'true')
9090
const lintMdAction = new LintMdAction()
9191
// lint
9292
await lintMdAction.lint()
9393
expect(lintMdAction.isPass()).toStrictEqual(false)
9494
})
95+
96+
test('用户传入了多个以空格分割的检测目录', async () => {
97+
process.env.GITHUB_WORKSPACE = path.resolve(process.cwd(), 'examples')
98+
mockAction('./js-config ./no-config-file', null, 'true')
99+
const lintMdAction = new LintMdAction()
100+
// lint
101+
await lintMdAction.lint()
102+
lintMdAction.showResult()
103+
lintMdAction.showErrorOrPassInfo()
104+
const totalErrors = lintMdAction.getErrors()
105+
expect(totalErrors.length).toStrictEqual(2)
106+
})
95107
})

dist/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lint-md-action.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class LintMdAction {
3535

3636
// JavaScript 模块,直接 require
3737
if (configPath.endsWith('.js')) {
38-
return require(configPath)
38+
return require(`${configPath}`)
3939
}
4040
const content = fs.readFileSync(configPath).toString()
4141
return JSON.parse(content)
@@ -44,7 +44,8 @@ class LintMdAction {
4444
isPass() {
4545
const result = this.linter ? this.linter.errorCount() : {}
4646
const noErrorAndWarn = result.error === 0 && result.warning === 0
47-
return core.getInput('failOnWarnings') ? noErrorAndWarn : result.error === 0
47+
// 注意这里的 getInput 返回值为 string
48+
return core.getInput('failOnWarnings') === 'true' ? noErrorAndWarn : result.error === 0
4849
}
4950

5051
async lint() {

0 commit comments

Comments
 (0)