move package name validation to TS part of action

This commit is contained in:
Aaron Trudeau
2022-02-28 22:26:50 -05:00
parent 7a6805e756
commit a04ac8389d
7 changed files with 157 additions and 25 deletions

View File

@@ -1,6 +1,9 @@
import Input from './input';
import fs from 'fs';
jest.mock('./unity-version-parser');
jest.mock('fs');
const mockedFs = fs as jest.Mocked<typeof fs>;
describe('Input', () => {
describe('getFromUser', () => {
@@ -33,4 +36,63 @@ describe('Input', () => {
expect(Input.isValidFolderName(folderName)).toStrictEqual(false);
});
});
describe('getPackageNameFromPackageJson', () => {
it('throws error if package.json cannot be found at the given project path', () => {
mockedFs.existsSync.mockReturnValue(false);
expect(() => Input.getPackageNameFromPackageJson('some/path')).toThrow(
'Invalid projectPath - Cannot find package.json at some/path/package.json',
);
});
it('throws error if package.json contents cannot be parsed', () => {
mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(Buffer.from('DefinitelyNotJSON'));
expect(() => Input.getPackageNameFromPackageJson('some/path')).toThrow(
/Unable to parse package.json contents as JSON/,
);
});
it('throws error if name field in package.json is not present', () => {
mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(
Buffer.from(JSON.stringify({ notName: 'some-package', alsoNotName: 'some-package' })),
);
expect(() => Input.getPackageNameFromPackageJson('some/path')).toThrow(
'Unable to parse package name from package.json - package name should be string, but was undefined',
);
});
it('throws error if name field in package.json is present but not a string', () => {
mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(
Buffer.from(JSON.stringify({ name: 3, notName: 'some-package' })),
);
expect(() => Input.getPackageNameFromPackageJson('some/path')).toThrow(
'Unable to parse package name from package.json - package name should be string, but was number',
);
});
it('throws error if name field in package.json is present but empty', () => {
mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(Buffer.from(JSON.stringify({ name: '', notName: 3 })));
expect(() => Input.getPackageNameFromPackageJson('some/path')).toThrow(
'Package name from package.json is a string, but is empty',
);
});
it('returns the name field in package.json if it is present as a non-empty string', () => {
mockedFs.existsSync.mockReturnValue(true);
mockedFs.readFileSync.mockReturnValue(
Buffer.from(JSON.stringify({ name: 'some-package', notName: 'not-what-we-want' })),
);
expect(Input.getPackageNameFromPackageJson('some/path')).toStrictEqual('some-package');
});
});
});