Skip to content

Commit

Permalink
new CI rule with tests (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
GantMan authored Dec 1, 2018
1 parent bea63b6 commit e4f46bf
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 57 deletions.
12 changes: 7 additions & 5 deletions __tests__/command_helpers/getSolidarityHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('Test helper functions', () => {
expect(isURI('/nachos')).toBeFalsy()
expect(isURI('./nachos')).toBeFalsy()
})

})

describe('loadFile', () => {
Expand Down Expand Up @@ -50,13 +49,16 @@ describe('Test helper functions', () => {
})

test('loadWebCheck positive cases', async () => {
expect(await loadWebCheck(context, 'https://raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/react-native.solidarity')).toBeTruthy()
expect(
await loadWebCheck(
context,
'https://raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/react-native.solidarity'
)
).toBeTruthy()
})

test('loadWebCheck false cases', async () => {
await expect(loadWebCheck(context, 'https://raw.githubusercontent.com/fail/sauce'))
.rejects
.toThrow()
await expect(loadWebCheck(context, 'https://raw.githubusercontent.com/fail/sauce')).rejects.toThrow()
})
})
})
23 changes: 7 additions & 16 deletions __tests__/command_helpers/getSolidaritySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe('basic getSolidaritySettings', () => {

describe('w/ failure', () => {
test('getSolidaritySettings can fail', async () => {

// Original sync style
// expect(() => {
// process.chdir('__tests__')
Expand All @@ -32,17 +31,13 @@ describe('basic getSolidaritySettings', () => {
// process.chdir('../')

process.chdir('__tests__')
await expect(getSolidaritySettings(context))
.rejects
.toThrow()
await expect(getSolidaritySettings(context)).rejects.toThrow()
process.chdir('../')
})

test('getSolidaritySettings can warn with missing requirements', async () => {
process.chdir('__tests__/sandbox/solidarity_broken')
await expect(getSolidaritySettings(context))
.rejects
.toThrow()
await expect(getSolidaritySettings(context)).rejects.toThrow()
process.chdir('../../../')
})
})
Expand All @@ -68,15 +63,11 @@ describe('parameterized getSolidaritySettings', () => {
test('failing path message', async () => {
// test longhand
context.parameters.options = { solidarityFile: '__tests__/fake' }
await expect(getSolidaritySettings(context))
.rejects
.toThrow('ERROR: There is no solidarity file at the given path')
await expect(getSolidaritySettings(context)).rejects.toThrow('ERROR: There is no solidarity file at the given path')

// test shorthand
context.parameters.options = { f: '__tests__/fake' }
await expect(getSolidaritySettings(context))
.rejects
.toThrow('ERROR: There is no solidarity file at the given path')
await expect(getSolidaritySettings(context)).rejects.toThrow('ERROR: There is no solidarity file at the given path')

context.parameters.options = {}
})
Expand Down Expand Up @@ -112,9 +103,9 @@ describe('parameterized getSolidaritySettings', () => {

test('errors if no solidarity file in module', async () => {
context.parameters.options = { module: 'nope' }
await expect(getSolidaritySettings(context))
.rejects
.toThrow('ERROR: There is no solidarity file found with the given module');
await expect(getSolidaritySettings(context)).rejects.toThrow(
'ERROR: There is no solidarity file found with the given module'
)
context.parameters.options = {}
})

Expand Down
2 changes: 1 addition & 1 deletion __tests__/command_helpers/getVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ describe('getVersion', () => {
} catch (e) {
result = e
}
expect(result).toEqual("No version identifier flag for this binary was found")
expect(result).toEqual('No version identifier flag for this binary was found')
})
})
32 changes: 21 additions & 11 deletions __tests__/command_helpers/skipRule.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import skipRule from '../../src/extensions/functions/skipRule'

const currentPlatform = process.platform
const makeMockRulePlatform = platform => ({ platform })
const mockRuleBasic = makeMockRulePlatform(currentPlatform)
const mockRuleUppercase = makeMockRulePlatform(currentPlatform.toUpperCase())

test('skipRule takes a string', () => {
expect(skipRule(currentPlatform)).toBe(false)
expect(skipRule(currentPlatform.toUpperCase())).toBe(false)
expect(skipRule(mockRuleBasic)).toBe(false)
expect(skipRule(mockRuleUppercase)).toBe(false)
if (currentPlatform === 'darwin') {
expect(skipRule('macos')).toBe(false)
expect(skipRule(makeMockRulePlatform('macos'))).toBe(false)
} else if (currentPlatform === 'win32') {
expect(skipRule('windows')).toBe(false)
expect(skipRule(makeMockRulePlatform('windows'))).toBe(false)
}
})

test('skipRule takes an array', () => {
const arrayOfOne = [currentPlatform]
expect(skipRule(arrayOfOne)).toBe(false)
expect(skipRule(makeMockRulePlatform(arrayOfOne))).toBe(false)
const arrayOfMore = [currentPlatform, 'nachos', 'tacos']
expect(skipRule(arrayOfMore)).toBe(false)
expect(skipRule(makeMockRulePlatform(arrayOfMore))).toBe(false)
if (currentPlatform === 'darwin') {
expect(skipRule(['macos', 'nachos', 'tacos'])).toBe(false)
expect(skipRule(makeMockRulePlatform(['macos', 'nachos', 'tacos']))).toBe(false)
} else if (currentPlatform === 'win32') {
expect(skipRule(['windows', 'nachos', 'tacos'])).toBe(false)
expect(skipRule(makeMockRulePlatform(['windows', 'nachos', 'tacos']))).toBe(false)
}
})

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

test('skips on platform miss', () => {
expect(skipRule('nachos')).toBe(true)
expect(skipRule(['nachos'])).toBe(true)
expect(skipRule(['nachos', 'tacos'])).toBe(true)
expect(skipRule(makeMockRulePlatform('nachos'))).toBe(true)
expect(skipRule(makeMockRulePlatform(['nachos']))).toBe(true)
expect(skipRule(makeMockRulePlatform(['nachos', 'tacos']))).toBe(true)
})

test('skips CI when flagged', () => {
const onCI = !!process.env.CI
expect(skipRule({ ci: true })).toBe(false)
expect(skipRule({})).toBe(false)
expect(skipRule({ ci: false })).toBe(onCI)
})
14 changes: 10 additions & 4 deletions solidaritySchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
},
"matchIndex": { "type": "integer" },
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
"error": { "type": "string"}
"error": { "type": "string"},
"ci": { "type": "boolean" }
},
"required": ["rule", "binary"]
},
Expand All @@ -59,7 +60,8 @@
"properties": {
"rule": { "enum": [ "dir", "directory" ] },
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
"error": { "type": "string"}
"error": { "type": "string"},
"ci": { "type": "boolean" }
},
"required": ["rule", "location"]
},
Expand All @@ -69,7 +71,8 @@
"properties": {
"rule": { "enum": [ "file" ] },
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
"error": { "type": "string"}
"error": { "type": "string"},
"ci": { "type": "boolean" }
},
"required": ["rule", "location"]
},
Expand All @@ -79,7 +82,8 @@
"properties": {
"rule": { "enum": [ "env" ] },
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
"error": { "type": "string"}
"error": { "type": "string"},
"ci": { "type": "boolean" }
},
"required": ["rule", "variable"]
},
Expand All @@ -90,6 +94,7 @@
"rule": { "enum": [ "shell" ] },
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
"error": { "type": "string" },
"ci": { "type": "boolean" },
"match": { "type": "string", "description": "A regexp to search the output." }
},
"required": ["rule", "match"]
Expand All @@ -104,6 +109,7 @@
"name": { "type": "string" },
"platform": { "enum": ["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"] },
"error": { "type": "string" },
"ci": { "type": "boolean" },
"match": { "type": "string", "description": "A regexp to search the output." }
},
"required": ["rule", "plugin", "name"]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace Snapshot {

if (first) {
await buildSpecificRequirement(context)
.then(async (newRequirement) => {
.then(async newRequirement => {
const updatedSolidaritySettings = await appendSolidaritySettings(context, newRequirement)

setSolidaritySettings(updatedSolidaritySettings, context)
Expand Down
7 changes: 5 additions & 2 deletions src/extensions/functions/checkRequirement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ module.exports = async (

const { print } = context
const requirementName: string = head(requirement)
const rules: SolidarityRequirement = pipe(tail, flatten)(requirement)
const rules: SolidarityRequirement = pipe(
tail,
flatten
)(requirement)

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

switch (rule.rule) {
// Handle CLI rule check
Expand Down
20 changes: 9 additions & 11 deletions src/extensions/functions/getSolidarityHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as JSON5 from 'json5'
import * as path from 'path'

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

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

// Load check from web
const checkURL = isURI(checkOption) ? checkOption : `https:\/\/raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/${checkOption}.solidarity`
const checkURL = isURI(checkOption)
? checkOption
: `https:\/\/raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/${checkOption}.solidarity`
const result = await api.get(checkURL)
// console.log(result)
if (result.ok) {
checkSpinner && checkSpinner.succeed(`Found Stack: ${checkOption}`)
// Convert strings to JSON5 objects
const solidarityData = (typeof result.data === 'string')
? JSON5.parse(result.data)
: result.data
const solidarityData = typeof result.data === 'string' ? JSON5.parse(result.data) : result.data
return solidarityData
} else {
checkSpinner && checkSpinner.fail(`Unable to find a known tech stack for ${checkOption}`)
if (!silentMode) {
print.info(
`Check https://github.com/infinitered/solidarity-stacks for options.`
)
print.info(`Check https://github.com/infinitered/solidarity-stacks for options.`)
}
throw(`ERROR: Request failed (${result.status} - ${result.problem})`)
throw `ERROR: Request failed (${result.status} - ${result.problem})`
}
}

module.exports = {
isURI,
loadFile,
loadModule,
loadWebCheck
loadWebCheck,
}
9 changes: 6 additions & 3 deletions src/extensions/functions/reviewRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ module.exports = async (
const { print, solidarity } = context
const { colors, checkmark, xmark } = print
const prettyBool = (bl: boolean) => (bl ? checkmark + colors.green(' YES') : xmark + colors.red(' NO'))
// @ts-ignore - flatten will never get a string bc tail is called first
const rules: SolidarityRequirement = pipe(tail, flatten)(requirement)
const rules: SolidarityRequirement = pipe(
tail,
// @ts-ignore - flatten will never get a string bc tail is called first
flatten
)(requirement)
// check each rule for report
const ruleChecks = map(async (rule: SolidarityRule) => {
// Make sure this rule is active
if (skipRule(rule.platform)) return false
if (skipRule(rule)) return false

switch (rule.rule) {
// Handle CLI rule report
Expand Down
11 changes: 10 additions & 1 deletion src/extensions/functions/skipRule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { SolidarityRule } from '../../types'

// Return true if we should skip
module.exports = (platform: string | string[]): boolean => {
module.exports = (rule: SolidarityRule): boolean => {
let platform = rule.platform

// check Skip CI shortcut first
if (process.env.CI && rule.ci === false) {
return true
}

if (typeof platform === 'string') {
platform = platform.toLowerCase()
platform = platform === 'windows' ? 'win32' : platform
Expand Down
7 changes: 5 additions & 2 deletions src/extensions/functions/updateRequirement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ module.exports = async (

const { print } = context
const requirementName = head(requirement)
const rules = pipe(tail, flatten)(requirement)
const rules = pipe(
tail,
flatten
)(requirement)

let ruleString = ''
const spinner = print.spin(`Updating ${requirementName}`)

// check each rule for requirement
const ruleChecks = await map(async rule => {
// skip if we can't update
if (skipRule(rule.platform)) return []
if (skipRule(rule)) return []
switch (rule.rule) {
// Handle CLI rule update
case 'cli':
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,23 @@ export interface CLIRule {
readonly error?: string
readonly matchIndex?: number
readonly platform?: string | string[]
readonly ci?: boolean
}

export interface ENVRule {
readonly rule: 'env'
readonly variable: string
readonly error?: string
readonly platform?: string | string[]
readonly ci?: boolean
}

export interface FSRule {
readonly rule: 'dir' | 'directory' | 'file'
readonly location: string
readonly error?: string
readonly platform?: string | string[]
readonly ci?: boolean
}

/**
Expand Down Expand Up @@ -113,6 +116,10 @@ export interface ShellRule {
* An optional platform or platforms to target.
*/
readonly platform?: string | string[]
/**
* An optional flag to skip rule on CI
*/
readonly ci?: boolean
}

export interface CustomRule {
Expand All @@ -121,6 +128,7 @@ export interface CustomRule {
readonly name: string
readonly error?: string
readonly platform?: string | string[]
readonly ci?: boolean
}

// discriminated union for rule sets
Expand Down

0 comments on commit e4f46bf

Please sign in to comment.