-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate project config
srcDir
within project root (#951)
* Validate project config `srcDir` within project root Adds validation within `validateProjectConfig` to ensure the srcDir of a project points to either the project root, or a subdirectory. This will help ensure a user doesn't attempt to upload a parent or root directory by mistake. Adds a suite of jest tests to confirm our current validations, as well as the new validation logic. To support the testing with mocks around `process.exit`, added `return` statements to all the spots in `validateProjectConfig` where the whole node process would be existing anyway. Open to reverting those changes and using exceptions in the mocking of `process.exit` if that'd be cleaner / clearer. * Add validateProjectConfig to project dev command Also reworks the error message for srcOutsideProjectDir to be more informative and actionable * Fix validateProjectConfig tests * Tweak project validation error message based on DX feedback Drops including the "project root" filesystem path, and just has the message say project root.
- Loading branch information
1 parent
a08221d
commit df3e4cb
Showing
4 changed files
with
168 additions
and
4 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
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,144 @@ | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
const path = require('path'); | ||
const { EXIT_CODES } = require('../enums/exitCodes'); | ||
const projects = require('../projects'); | ||
|
||
describe('@hubspot/cli/lib/projects', () => { | ||
describe('validateProjectConfig()', () => { | ||
let realProcess; | ||
let projectDir; | ||
let exitMock; | ||
let errorSpy; | ||
|
||
beforeAll(() => { | ||
projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'projects-')); | ||
fs.mkdirSync(path.join(projectDir, 'src')); | ||
|
||
realProcess = process; | ||
errorSpy = jest.spyOn(console, 'error'); | ||
}); | ||
|
||
beforeEach(() => { | ||
exitMock = jest.fn(); | ||
global.process = { ...realProcess, exit: exitMock }; | ||
}); | ||
|
||
afterEach(() => { | ||
errorSpy.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
global.process = realProcess; | ||
errorSpy.mockRestore(); | ||
}); | ||
|
||
it('rejects undefined configuration', () => { | ||
projects.validateProjectConfig(null, projectDir); | ||
|
||
expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
expect.stringMatching(/.*config not found.*/) | ||
); | ||
}); | ||
|
||
it('rejects configuration with missing name', () => { | ||
projects.validateProjectConfig({ srcDir: '.' }, projectDir); | ||
|
||
expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
expect.stringMatching(/.*missing required fields*/) | ||
); | ||
}); | ||
|
||
it('rejects configuration with missing srcDir', () => { | ||
projects.validateProjectConfig({ name: 'hello' }, projectDir); | ||
|
||
expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
expect.stringMatching(/.*missing required fields.*/) | ||
); | ||
}); | ||
|
||
describe('rejects configuration with srcDir outside project directory', () => { | ||
it('for parent directory', () => { | ||
projects.validateProjectConfig( | ||
{ name: 'hello', srcDir: '..' }, | ||
projectDir | ||
); | ||
|
||
expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
expect.stringContaining('srcDir: ".."') | ||
); | ||
}); | ||
|
||
it('for root directory', () => { | ||
projects.validateProjectConfig( | ||
{ name: 'hello', srcDir: '/' }, | ||
projectDir | ||
); | ||
|
||
expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
expect.stringContaining('srcDir: "/"') | ||
); | ||
}); | ||
|
||
it('for complicated directory', () => { | ||
const srcDir = './src/././../src/../../src'; | ||
|
||
projects.validateProjectConfig({ name: 'hello', srcDir }, projectDir); | ||
|
||
expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
expect.stringContaining(`srcDir: "${srcDir}"`) | ||
); | ||
}); | ||
}); | ||
|
||
it('rejects configuration with srcDir that does not exist', () => { | ||
projects.validateProjectConfig( | ||
{ name: 'hello', srcDir: 'foo' }, | ||
projectDir | ||
); | ||
|
||
expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
expect.stringMatching(/.*could not be found in.*/) | ||
); | ||
}); | ||
|
||
describe('accepts configuration with valid srcDir', () => { | ||
it('for current directory', () => { | ||
projects.validateProjectConfig( | ||
{ name: 'hello', srcDir: '.' }, | ||
projectDir | ||
); | ||
|
||
expect(exitMock).not.toHaveBeenCalled(); | ||
expect(errorSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('for relative directory', () => { | ||
projects.validateProjectConfig( | ||
{ name: 'hello', srcDir: './src' }, | ||
projectDir | ||
); | ||
|
||
expect(exitMock).not.toHaveBeenCalled(); | ||
expect(errorSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('for implied relative directory', () => { | ||
projects.validateProjectConfig( | ||
{ name: 'hello', srcDir: 'src' }, | ||
projectDir | ||
); | ||
|
||
expect(exitMock).not.toHaveBeenCalled(); | ||
expect(errorSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
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