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

use path instead of posix-specific path #40

Merged
merged 3 commits into from
Jul 29, 2022
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
28 changes: 20 additions & 8 deletions src/frameworks/angular/angular-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ const getAngularJsonWorkspaceInfo = (
angularConfigRootPath: string,
logger: Logger
): AngularWorkspaceInfo | undefined => {
const angularJsonConfigPath = normalizePath(posix.resolve(angularConfigRootPath, 'angular.json'));
const angularJsonConfigPath = normalizePath(posix.join(angularConfigRootPath, 'angular.json'));

if (!existsSync(angularJsonConfigPath)) {
logger.debug(() => `Cannot get Angular projects - Angular Json file does not exist: ${angularJsonConfigPath}`);
return undefined;
}
const angularJson = JSON.parse(readFileSync(angularJsonConfigPath, 'utf-8'));
let angularJson: any;
try {
angularJson = JSON.parse(readFileSync(angularJsonConfigPath, 'utf-8'));
} catch (e) {
// do nothing
}

if (!angularJson) {
logger.debug(() => `Cannot get Angular projects - Failed to read Angular Json file: ${angularJsonConfigPath}`);
Expand All @@ -41,8 +46,10 @@ const getAngularJsonWorkspaceInfo = (
if (projectConfig.architect.test === undefined || projectConfig.architect.test.options.karmaConfig === undefined) {
continue;
}
const projectPath = posix.resolve(angularConfigRootPath, projectConfig.root);
const karmaConfigPath = posix.resolve(angularConfigRootPath, projectConfig.architect.test.options.karmaConfig);
const projectPath = normalizePath(posix.join(angularConfigRootPath, projectConfig.root));
const karmaConfigPath = normalizePath(
posix.join(angularConfigRootPath, projectConfig.architect.test.options.karmaConfig)
);

const project: AngularProjectInfo = {
name: projectName,
Expand Down Expand Up @@ -72,15 +79,20 @@ const getAngularCliJsonWorkspaceInfo = (
angularConfigRootPath: string,
logger: Logger
): AngularWorkspaceInfo | undefined => {
const angularCliJsonConfigPath = normalizePath(posix.resolve(angularConfigRootPath, '.angular-cli.json'));
const angularCliJsonConfigPath = normalizePath(posix.join(angularConfigRootPath, '.angular-cli.json'));

if (!existsSync(angularCliJsonConfigPath)) {
logger.debug(
() => `Cannot get Angular projects - Angular CLI Json file does not exist: ${angularCliJsonConfigPath}`
);
return undefined;
}
const angularCliJson = JSON.parse(readFileSync(angularCliJsonConfigPath, 'utf-8'));
let angularCliJson: any;
try {
angularCliJson = JSON.parse(readFileSync(angularCliJsonConfigPath, 'utf-8'));
} catch (e) {
// do nothing
}

if (!angularCliJson) {
logger.debug(
Expand All @@ -94,8 +106,8 @@ const getAngularCliJsonWorkspaceInfo = (

for (const app of angularCliJson.apps) {
const projectName: string = app.name || angularCliJson.project.name;
const projectPath = normalizePath(posix.resolve(angularConfigRootPath, app.root));
const karmaConfigPath = posix.resolve(angularConfigRootPath, angularCliJson.test.karma.config);
const projectPath = normalizePath(posix.join(angularConfigRootPath, app.root));
const karmaConfigPath = normalizePath(posix.join(angularConfigRootPath, angularCliJson.test.karma.config));

const project: AngularProjectInfo = {
name: projectName,
Expand Down
133 changes: 133 additions & 0 deletions test/frameworks/angular/angular-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { getAngularWorkspaceInfo } from '../../../src/frameworks/angular/angular-util';
import { normalizePath } from '../../../src/util/utils';

jest.mock('fs');
const fs = require('fs'); // require necessary for jest to work

const logger = {
debug: jest.fn(),
error: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
trace: jest.fn(),
dispose: jest.fn()
};

describe('angular-utils.getAngularWorkspaceInfo', () => {
const angularConfigRootPaths = [
['Unix', '/angular-config-root-path'],
['Windows', 'C:\\angular-config-root-path']
];

beforeEach(jest.clearAllMocks);

angularConfigRootPaths.forEach(item => {
const [platform, angularConfigRootPath] = item;

it(`should return undefined if no angular definition exists (${platform})`, () => {
fs.existsSync.mockReturnValue(false);
const result = getAngularWorkspaceInfo(angularConfigRootPath, logger);
expect(result).toBeUndefined();
expect(fs.existsSync).toHaveBeenCalledWith(normalizePath(`${angularConfigRootPath}/angular.json`));
expect(fs.existsSync).toHaveBeenCalledWith(normalizePath(`${angularConfigRootPath}/.angular-cli.json`));
});

it(`should return undefined if the angular definitions are invalid (${platform})`, () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockImplementation(() => 'invalid json');
const result = getAngularWorkspaceInfo(angularConfigRootPath, logger);
expect(result).toBeUndefined();
expect(fs.existsSync).toHaveBeenCalledWith(normalizePath(`${angularConfigRootPath}/angular.json`));
expect(fs.existsSync).toHaveBeenCalledWith(normalizePath(`${angularConfigRootPath}/.angular-cli.json`));
expect(fs.readFileSync).toHaveBeenCalledWith(normalizePath(`${angularConfigRootPath}/angular.json`), 'utf-8');
expect(fs.readFileSync).toHaveBeenCalledWith(
normalizePath(`${angularConfigRootPath}/.angular-cli.json`),
'utf-8'
);
});

it('should return a valid configuration from angular.json', () => {
fs.existsSync.mockImplementation((name: any) => (name as string).endsWith('angular.json'));
fs.readFileSync.mockImplementation(() =>
JSON.stringify({
// defaultProject: 'default-project', // deprecated!
projects: {
'project-1': {
root: 'project-1',
architect: {
test: {
options: {
karmaConfig: 'project-1/karma.conf.js'
}
}
}
},
'project-2': {
root: 'project-2',
architect: {
test: {
options: {
karmaConfig: 'project-2/karma.conf.js'
}
}
}
},
'project-3': {
root: 'project-3',
architect: {}
}
}
})
);
const result = getAngularWorkspaceInfo(angularConfigRootPath, logger);
expect(result).toBeDefined();
expect(result!.defaultProject).toEqual(result!.projects[0]);
expect(result!.projects.length).toBe(2);
expect(result!.projects[0].name).toBe('project-1');
expect(result!.projects[0].rootPath).toBe(normalizePath(angularConfigRootPath + '/project-1'));
expect(result!.projects[0].karmaConfigPath).toBe(
normalizePath(angularConfigRootPath + '/project-1/karma.conf.js')
);
expect(result!.projects[1].name).toBe('project-2');
expect(result!.projects[1].rootPath).toBe(normalizePath(angularConfigRootPath + '/project-2'));
expect(result!.projects[1].karmaConfigPath).toBe(
normalizePath(angularConfigRootPath + '/project-2/karma.conf.js')
);
});

it('should return a valid configuration from .angular-cli.json', () => {
fs.existsSync.mockImplementation((name: any) => (name as string).endsWith('.angular-cli.json'));
fs.readFileSync.mockImplementation(() =>
JSON.stringify({
project: {
name: 'my-app'
},
apps: [
{
root: 'src-1'
},
{
name: 'my-app-2',
root: 'src-2'
}
],
test: {
karma: {
config: './karma.conf.js'
}
}
})
);
const result = getAngularWorkspaceInfo(angularConfigRootPath, logger);
expect(result).toBeDefined();
expect(result!.defaultProject).toEqual(result!.projects[0]);
expect(result!.projects.length).toBe(2);
expect(result!.projects[0].name).toBe('my-app');
expect(result!.projects[0].rootPath).toBe(normalizePath(angularConfigRootPath + '/src-1'));
expect(result!.projects[0].karmaConfigPath).toBe(normalizePath(angularConfigRootPath + '/karma.conf.js'));
expect(result!.projects[1].name).toBe('my-app-2');
expect(result!.projects[1].rootPath).toBe(normalizePath(angularConfigRootPath + '/src-2'));
expect(result!.projects[1].karmaConfigPath).toBe(normalizePath(angularConfigRootPath + '/karma.conf.js'));
});
});
});