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

fix when you create-amplify absolute path, raised error #1071

Merged
5 changes: 5 additions & 0 deletions .changeset/odd-dolphins-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-amplify': patch
---

fix: when you do create-amplify absolute path, raised error and log.
44 changes: 44 additions & 0 deletions packages/create-amplify/src/get_project_root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import fsp from 'fs/promises';
import path from 'path';
import { AmplifyPrompter } from '@aws-amplify/cli-core';
import { getProjectRoot } from './get_project_root.js';
import { AmplifyError, AmplifyUserError } from '@aws-amplify/platform-core';
import util from 'util';

const originalEnv = process.env;

Expand Down Expand Up @@ -72,6 +74,48 @@ void describe('getProjectRoot', () => {
assert.equal(projectRoot, path.resolve(userInput));
});

void it('prints warning if creation of project root failed and path is absolute', async () => {
process.env.npm_config_yes = 'false';
const userInput = 'some/absolute/path';
mock.method(AmplifyPrompter, 'input', () => Promise.resolve(userInput));
const expectedError = new AmplifyUserError('ProjectDirectoryCreateError', {
message: `Failed to create project directory`,
resolution: `Ensure that ${userInput} is the correct path and you have write permissions to this location.`,
});
fsMkDirSyncMock.mock.mockImplementationOnce(() =>
Promise.reject(expectedError)
);
await assert.rejects(
() => getProjectRoot(),
(err: Error) => {
const sampleStderr = `some random stderr
before the actual error message
${util.inspect(err, { depth: null })}
and some after the error message`;

const actual = AmplifyError.fromStderr(sampleStderr);
assert.deepStrictEqual(actual?.name, expectedError.name);
assert.deepStrictEqual(
actual?.classification,
expectedError.classification
);
assert.deepStrictEqual(actual?.message, expectedError.message);
assert.deepStrictEqual(actual?.details, expectedError.details);
assert.deepStrictEqual(actual?.cause?.name, expectedError.cause?.name);
assert.deepStrictEqual(
actual?.cause?.message,
expectedError.cause?.message
);
return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need all this. Just assert on the err that the name, message and resolution is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Amplifiyer
thank you feedback!!!!!
I try change test case

}
);
assert.equal(fsMkDirSyncMock.mock.callCount(), 1);
assert.equal(
fsMkDirSyncMock.mock.calls[0].arguments[0],
path.resolve(userInput)
);
});

void it('use default options if `yes`', async (ctx) => {
process.env.npm_config_yes = 'false';
process.argv = ['node', 'test.js', '--yes'];
Expand Down
10 changes: 9 additions & 1 deletion packages/create-amplify/src/get_project_root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import yargs from 'yargs';
import { AmplifyPrompter, LogLevel } from '@aws-amplify/cli-core';
import { printer } from './printer.js';
import { AmplifyUserError } from '@aws-amplify/platform-core';

/**
* Returns the project root directory.
Expand Down Expand Up @@ -37,7 +38,14 @@ export const getProjectRoot = async () => {
LogLevel.DEBUG
);
printer.log(`Creating directory ${projectRoot}`, LogLevel.DEBUG);
await fsp.mkdir(projectRoot, { recursive: true });
try {
await fsp.mkdir(projectRoot, { recursive: true });
} catch (err) {
throw new AmplifyUserError('ProjectDirectoryCreateError', {
message: `Failed to create project directory`,
resolution: `Ensure that ${projectRoot} is the correct path and you have write permissions to this location.`,
});
}
}
return projectRoot;
};