Skip to content

Commit

Permalink
break out helpers and write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GantMan committed Aug 10, 2018
1 parent 2edb198 commit 749c1a9
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 58 deletions.
50 changes: 50 additions & 0 deletions __tests__/command_helpers/getSolidarityHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { isURI, loadFile, loadModule, loadWebCheck } from '../../src/extensions/functions/getSolidarityHelpers'

const context = require('mockContext')

describe('Test helper functions', () => {
describe('isURI', () => {
test('isURI positive case', () => {
expect(isURI('http://www.google.com')).toBeTruthy()
expect(isURI('https://www.google.com')).toBeTruthy()
})

test('isURI fail case', () => {
expect(isURI('nachos')).toBeFalsy()
expect(isURI('/nachos')).toBeFalsy()
expect(isURI('./nachos')).toBeFalsy()
})

})

describe('loadFile', () => {
test('loadFile positive cases', () => {
expect(loadFile(context, '__tests__/sandbox/solidarity_json')).toBeTruthy()
expect(loadFile(context, '__tests__/sandbox/solidarity_json/.solidarity.json')).toBeTruthy()
})

test('loadFile false cases', () => {
expect(() => {
loadFile(context, '__tests__/sandbox/fake_project')
}).toThrow()
expect(() => {
loadFile(context, '__tests__/sandbox/fake_project/nope.solidarity')
}).toThrow()
})
})

// describe('loadModule', () => {
// })

describe('loadWebCheck', () => {
test('loadWebCheck positive cases', async () => {
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/infinitered/solidarity-stacks/master/stacks/failsauce'))
.rejects
.toThrow()
})
})
})
2 changes: 1 addition & 1 deletion __tests__/command_helpers/getSolidaritySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('basic getSolidaritySettings', () => {
test('getSolidaritySettings can fail', async () => {

// Original sync style
// expect(async () => {
// expect(() => {
// process.chdir('__tests__')
// getSolidaritySettings(context)
// }).toThrow()
Expand Down
66 changes: 66 additions & 0 deletions src/extensions/functions/getSolidarityHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as JSON5 from 'json5'
import * as path from 'path'

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

const loadFile = (context, filePath) => {
const { filesystem } = context
if (filesystem.exists(filePath) === 'file') {
return JSON5.parse(filesystem.read(filePath))
} else if (filesystem.exists(filePath + path.sep + '.solidarity')) {
return JSON5.parse(filesystem.read(filePath + path.sep + '.solidarity'))
} else if (filesystem.exists(filePath + path.sep + '.solidarity.json')) {
return JSON5.parse(filesystem.read(filePath + path.sep + '.solidarity.json'))
} else {
throw 'ERROR: There is no solidarity file at the given path'
}
}

const loadModule = (context, moduleName) => {
const { filesystem } = context
// We will search that module
const filePath = path.join('node_modules', moduleName, '.solidarity')

if (filesystem.exists(filePath)) {
return JSON5.parse(filesystem.read(filePath))
} else if (filesystem.exists(filePath + '.json')) {
return JSON5.parse(filesystem.read(filePath + '.json'))
} else {
throw 'ERROR: There is no solidarity file found with the given module'
}
}

const loadWebCheck = async (context, checkOption) => {
const { print, http } = context
const checkSpinner = print.spin(`Running check on ${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'
})

// Load check from web
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.succeed(`Found Stack: ${checkOption}`)
// Convert strings to JSON5 objects
const solidarityData = (typeof result.data === 'string')
? JSON5.parse(result.data)
: result.data
return solidarityData
} else {
checkSpinner.fail(`Unable to find a known tech stack for ${checkOption}`)
print.info(
`Check https://github.com/infinitered/solidarity-stacks for options.`
)
throw(`ERROR: Request failed (${result.status} - ${result.problem})`)
}
}

module.exports = {
isURI,
loadFile,
loadModule,
loadWebCheck
}
58 changes: 1 addition & 57 deletions src/extensions/functions/getSolidaritySettings.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,6 @@
import { SolidarityRunContext, SolidaritySettings } from '../../types'
import { loadFile, loadModule, loadWebCheck } from './getSolidarityHelpers'
import * as JSON5 from 'json5'
import * as path from 'path'

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

export const loadFile = (context, filePath) => {
const { filesystem } = context
if (filesystem.exists(filePath + path.sep + '.solidarity')) {
return JSON5.parse(filesystem.read(filePath + path.sep + '.solidarity'))
} else if (filesystem.exists(filePath + path.sep + '.solidarity.json')) {
return JSON5.parse(filesystem.read(filePath + path.sep + '.solidarity.json'))
} else if (filesystem.exists(filePath) === 'file') {
return JSON5.parse(filesystem.read(filePath))
} else {
throw 'ERROR: There is no solidarity file at the given path'
}
}

export const loadModule = (context, moduleName) => {
const { filesystem } = context
// We will search that module
const filePath = path.join('node_modules', moduleName, '.solidarity')

if (filesystem.exists(filePath)) {
return JSON5.parse(filesystem.read(filePath))
} else if (filesystem.exists(filePath + '.json')) {
return JSON5.parse(filesystem.read(filePath + '.json'))
} else {
throw 'ERROR: There is no solidarity file found with the given module'
}
}

export const loadWebCheck = async (context, checkOption) => {
const { print, http } = context
const checkSpinner = print.spin(`Running check on ${checkOption}`)
const api = http.create({
baseURL: 'https://api.github.com'
})

// Load check from web
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.succeed(`Found Stack: ${checkOption}`)
// Convert strings to JSON5 objects
const solidarityData = (typeof result.data === 'string')
? JSON5.parse(result.data)
: result.data
return solidarityData
} else {
checkSpinner.fail(`Unable to find a known tech stack for ${checkOption}`)
print.info(
`Check https://github.com/infinitered/solidarity-stacks for options.`
)
throw(`ERROR: Request failed (${result.status} - ${result.problem})`)
}
}

module.exports = async (context: SolidarityRunContext): Promise<SolidaritySettings> => {
const { filesystem, parameters } = context
Expand Down

0 comments on commit 749c1a9

Please sign in to comment.