-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
118 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters