Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate project config srcDir within project root #951

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/cli/commands/project/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
handleProjectUpload,
pollProjectBuildAndDeploy,
showPlatformVersionWarning,
validateProjectConfig,
} = require('../../lib/projects');
const { EXIT_CODES } = require('../../lib/enums/exitCodes');
const {
Expand Down Expand Up @@ -87,6 +88,8 @@ exports.handler = async options => {
process.exit(EXIT_CODES.ERROR);
}

validateProjectConfig(projectConfig, projectDir);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callout for the top level PR. This addition is intentional.
The dev command should be validating the project configuration for use later on.


const accounts = getConfigAccounts();
let targetAccountId = options.account ? accountId : null;
let createNewSandbox = false;
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,8 @@ en:
startError: "Failed to start local dev server: {{ message }}"
fileChangeError: "Failed to notify local dev server of file change: {{ message }}"
projects:
config:
srcOutsideProjectDir: "Invalid value for 'srcDir' in {{ projectConfig }}: {{#bold}}srcDir: \"{{ srcDir }}\"{{/bold}}\n\t'srcDir' must be a relative path to a folder under the project root, such as \".\" or \"./src\""
uploadProjectFiles:
add: "Uploading {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
fail: "Failed to upload {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
Expand Down
144 changes: 144 additions & 0 deletions packages/cli/lib/__tests__/projects.test.js
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'));
mendel-at-hubspot marked this conversation as resolved.
Show resolved Hide resolved

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();
});
});
});
});
23 changes: 19 additions & 4 deletions packages/cli/lib/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,36 @@ const validateProjectConfig = (projectConfig, projectDir) => {
logger.error(
`Project config not found. Try running 'hs project create' first.`
);
process.exit(EXIT_CODES.ERROR);
return process.exit(EXIT_CODES.ERROR);
}

if (!projectConfig.name || !projectConfig.srcDir) {
logger.error(
'Project config is missing required fields. Try running `hs project create`.'
);
process.exit(EXIT_CODES.ERROR);
return process.exit(EXIT_CODES.ERROR);
}

const resolvedPath = path.resolve(projectDir, projectConfig.srcDir);
if (!resolvedPath.startsWith(projectDir)) {
const projectConfigFile = path.relative(
'.',
path.join(projectDir, PROJECT_CONFIG_FILE)
);
logger.error(
i18n(`${i18nKey}.config.srcOutsideProjectDir`, {
srcDir: projectConfig.srcDir,
projectConfig: projectConfigFile,
})
);
return process.exit(EXIT_CODES.ERROR);
}

if (!fs.existsSync(path.resolve(projectDir, projectConfig.srcDir))) {
if (!fs.existsSync(resolvedPath)) {
logger.error(
`Project source directory '${projectConfig.srcDir}' could not be found in ${projectDir}.`
);
process.exit(EXIT_CODES.ERROR);
return process.exit(EXIT_CODES.ERROR);
}
};

Expand Down
Loading