Skip to content

Commit 60ae218

Browse files
authored
configurable solidarity file locations with -f and -m (#199)
* configurable solidarity file locations * fix tests * adding testing for new feature * attempt fixing CI * CI is a pain * override gitignore for tests
1 parent 56bd42e commit 60ae218

File tree

7 files changed

+139
-18
lines changed

7 files changed

+139
-18
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`getSolidaritySettings w/ success getSolidaritySettings exists 1`] = `[Function]`;
3+
exports[`basic getSolidaritySettings w/ success getSolidaritySettings exists 1`] = `[Function]`;
Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { solidarity } from '../../src'
22
import getSolidaritySettings from '../../src/extensions/functions/getSolidaritySettings'
33

4-
const context = require('gluegun/toolbox')
4+
const context = require('mockContext')
55

6-
describe('getSolidaritySettings', () => {
6+
describe('basic getSolidaritySettings', () => {
77
describe('w/ success', () => {
88
test('getSolidaritySettings exists', () => expect(getSolidaritySettings).toMatchSnapshot())
99

10-
test('getSolidaritySettings succeeds', async () => {
10+
test('getSolidaritySettings succeeds', () => {
1111
const resultSettings = getSolidaritySettings(context)
1212
// we got an object with requirements defined
1313
expect(resultSettings).toMatchObject({ requirements: {} })
1414
})
1515

16-
test('getSolidaritySettings succeeds', async () => {
16+
test('getSolidaritySettings succeeds', () => {
1717
process.chdir('__tests__/sandbox/solidarity_json')
1818
const resultSettings = getSolidaritySettings(context)
1919
// we got an object with requirements defined
@@ -22,19 +22,98 @@ describe('getSolidaritySettings', () => {
2222
})
2323
})
2424

25-
test('getSolidaritySettings can fail', async () => {
25+
describe('w/ failure', () => {
26+
test('getSolidaritySettings can fail', () => {
27+
expect(() => {
28+
process.chdir('__tests__')
29+
const resultSettings = getSolidaritySettings(context)
30+
}).toThrow()
31+
process.chdir('../')
32+
})
33+
34+
test('getSolidaritySettings can warn with missing requirements', () => {
35+
expect(() => {
36+
process.chdir('__tests__/sandbox/solidarity_broken')
37+
const resultSettings = getSolidaritySettings(context)
38+
}).toThrowError('ERROR: Found, but no requirements key. Please validate your solidarity file')
39+
process.chdir('../../../')
40+
})
41+
}
42+
})
43+
44+
describe('parameterized getSolidaritySettings', () => {
45+
46+
test('custom path with -f', () => {
47+
context.parameters.options = { f: '__tests__/sandbox/solidarity_json' }
48+
const resultSettings = getSolidaritySettings(context)
49+
// we got an object with requirements defined
50+
expect(resultSettings).toMatchObject({ requirements: {} })
51+
context.parameters.options = {}
52+
})
53+
54+
test('custom path with --solidarityFile', () => {
55+
context.parameters.options = { solidarityFile: '__tests__/sandbox/solidarity_json' }
56+
const resultSettings = getSolidaritySettings(context)
57+
// we got an object with requirements defined
58+
expect(resultSettings).toMatchObject({ requirements: {} })
59+
context.parameters.options = {}
60+
})
61+
62+
test('failing path message', () => {
63+
// test longhand
64+
context.parameters.options = { solidarityFile: '__tests__/fake' }
2665
expect(() => {
27-
process.chdir('__tests__')
2866
const resultSettings = getSolidaritySettings(context)
29-
}).toThrow()
30-
process.chdir('../')
67+
}).toThrowError('ERROR: There is no solidarity file at the given path')
68+
// test shorthand
69+
context.parameters.options = { f: '__tests__/fake' }
70+
expect(() => {
71+
const resultSettings = getSolidaritySettings(context)
72+
}).toThrowError('ERROR: There is no solidarity file at the given path')
73+
context.parameters.options = {}
3174
})
3275

33-
test('getSolidaritySettings can warn with missing requirements', async () => {
34-
expect(() => {
35-
process.chdir('__tests__/sandbox/solidarity_broken')
76+
describe('custom module tests', () => {
77+
beforeAll(() => {
78+
process.chdir('__tests__/sandbox/fake_project')
79+
})
80+
81+
test('can find solidarity file in module with flag -m', () => {
82+
context.parameters.options = { m: 'mock_module' }
83+
const resultSettings = getSolidaritySettings(context)
84+
// we got an object with requirements defined
85+
expect(resultSettings).toMatchObject({ requirements: {} })
86+
context.parameters.options = {}
87+
})
88+
89+
test('can find solidarity file in module with flag --module', () => {
90+
context.parameters.options = { module: 'mock_module' }
3691
const resultSettings = getSolidaritySettings(context)
37-
}).toThrowError('ERROR: Found, but no requirements key. Please validate your solidarity file')
38-
process.chdir('../../../')
92+
// we got an object with requirements defined
93+
expect(resultSettings).toMatchObject({ requirements: {} })
94+
context.parameters.options = {}
95+
})
96+
97+
test('can find solidarity JSON file in module with flag --module', () => {
98+
context.parameters.options = { module: 'mock_second_module' }
99+
const resultSettings = getSolidaritySettings(context)
100+
// we got an object with requirements defined
101+
expect(resultSettings).toMatchObject({ requirements: {} })
102+
context.parameters.options = {}
103+
})
104+
105+
test('errors if no solidarity file in module', () => {
106+
context.parameters.options = { module: 'nope' }
107+
expect(() => {
108+
const resultSettings = getSolidaritySettings(context)
109+
}).toThrowError('ERROR: There is no solidarity file found with the given module')
110+
context.parameters.options = {}
111+
})
112+
113+
afterAll(() => {
114+
process.chdir('../../../')
115+
})
116+
39117
})
118+
40119
})

__tests__/commands/help.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test('Calls print items several times', () => {
2727
expect(context.print.success.mock.calls.length).toBe(0)
2828
expect(context.print.colors.magenta.mock.calls.length).toBe(0)
2929
helpCommand.run(context)
30-
expect(context.print.info.mock.calls.length).toBe(6)
30+
expect(context.print.info.mock.calls.length).toBe(8)
3131
expect(context.print.printCommands.mock.calls.length).toBe(1)
3232
expect(context.print.success.mock.calls.length).toBe(2)
3333
expect(context.print.colors.magenta.mock.calls.length).toBe(2)

__tests__/sandbox/fake_project/node_modules/mock_module/.solidarity

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

__tests__/sandbox/fake_project/node_modules/mock_second_module/.solidarity.json

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

src/commands/help.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module.exports = {
1313
print.info(' --verbose\t (-a) Prints all detected info during solidarity check')
1414
print.info(' --moderate\t (-m) Prints failures in check or single success message')
1515
print.info(' --silent\t (-s) No output, just a return code of success/failure')
16+
print.info(' --solidarityFile\t (-f) Use given path to solidarity file for settings')
17+
print.info(' --module\t (-m) Search for a solidarity file in the given npm package')
1618

1719
print.success(colors.magenta('\nSolidarity is open source - https://github.com/infinitered/solidarity'))
1820
print.info(colors.magenta('If you need additional help, join our Slack at http://community.infinite.red'))

src/extensions/functions/getSolidaritySettings.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
import { SolidarityRunContext, SolidaritySettings } from '../../types'
22
import * as JSON5 from 'json5'
3+
import * as path from 'path'
4+
35
module.exports = (context: SolidarityRunContext): SolidaritySettings => {
4-
const { filesystem } = context
6+
const { filesystem, parameters } = context
7+
const options = parameters.options || {} // fix possibly undefined from gluegun
58

6-
// for now only JSON and JSON5 support
9+
/* for now only JSON and JSON5 support
10+
* Summary:
11+
* Looks for `.solidarity` or `.solidarity.json` files
12+
* Unless you pass parameter options telling us to look
13+
* in specific paths or node modules
14+
*/
715
let solidaritySettings
8-
if (filesystem.exists('.solidarity')) {
16+
if (options.solidarityFile || options.f) {
17+
// They are telling us where to go
18+
const filePath = options.solidarityFile || options.f
19+
if (filesystem.exists(filePath)) {
20+
solidaritySettings = JSON5.parse(filesystem.read('.solidarity'))
21+
} else {
22+
throw 'ERROR: There is no solidarity file at the given path'
23+
}
24+
} else if (options.module || options.m) {
25+
// We will search that module
26+
const moduleName = options.module || options.m
27+
const filePath = path.join('node_modules', moduleName, '.solidarity')
28+
29+
if (filesystem.exists(filePath)) {
30+
solidaritySettings = JSON5.parse(filesystem.read(filePath))
31+
} else if (filesystem.exists(filePath + '.json')) {
32+
solidaritySettings = JSON5.parse(filesystem.read(filePath + '.json'))
33+
} else {
34+
throw 'ERROR: There is no solidarity file found with the given module'
35+
}
36+
} else if (filesystem.exists('.solidarity')) {
937
solidaritySettings = JSON5.parse(filesystem.read('.solidarity'))
1038
} else if (filesystem.exists('.solidarity.json')) {
1139
solidaritySettings = JSON5.parse(filesystem.read('.solidarity.json'))

0 commit comments

Comments
 (0)