Skip to content

Commit

Permalink
Read proper installed package version
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed May 19, 2023
1 parent 2547f92 commit 9f43b06
Show file tree
Hide file tree
Showing 55 changed files with 1,465 additions and 898 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import type { StorybookConfig } from '@storybook/types';
import type { PackageJson } from '../../js-package-manager';
import { makePackageManager, mockStorybookData } from '../helpers/testing-helpers';
import type { JsPackageManager } from '../../js-package-manager';
import { angularBuildersMultiproject } from './angular-builders-multiproject';
import * as helpers from '../../helpers';
import * as angularHelpers from '../../generators/ANGULAR/helpers';

const checkAngularBuilders = async ({
packageJson,
main: mainConfig = {},
storybookVersion = '7.0.0',
packageManager,
mainConfig = {},
}: {
packageJson: PackageJson;
main?: Partial<StorybookConfig> & Record<string, unknown>;
storybookVersion?: string;
packageManager: Partial<JsPackageManager>;
mainConfig?: Partial<StorybookConfig>;
}) => {
mockStorybookData({ mainConfig, storybookVersion });

// mock file system (look at eslint plugin test)

return angularBuildersMultiproject.check({
packageManager: makePackageManager(packageJson),
packageManager: packageManager as any,
mainConfig: mainConfig as any,
storybookVersion: '7.0.0',
});
};

Expand All @@ -34,51 +29,69 @@ jest.mock('../../generators/ANGULAR/helpers', () => ({
}));

describe('is Nx project', () => {
const packageManager = {
getPackageVersion: () => {
return null;
},
} as Partial<JsPackageManager>;

beforeEach(() => {
(helpers.isNxProject as any as jest.SpyInstance).mockReturnValue(true);
(helpers.isNxProject as any as jest.SpyInstance).mockResolvedValue(true);
});

it('should return null', async () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0' },
};

await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
await expect(checkAngularBuilders({ packageManager })).resolves.toBeNull();
});
});

describe('is not Nx project', () => {
beforeEach(() => {
(helpers.isNxProject as any as jest.SpyInstance).mockReturnValue(false);
(helpers.isNxProject as any as jest.SpyInstance).mockResolvedValue(false);
});

describe('angular builders', () => {
afterEach(jest.restoreAllMocks);

describe('Angular not found', () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0' },
};
const packageManager = {
getPackageVersion: jest.fn().mockResolvedValue(null),
} as Partial<JsPackageManager>;

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
await expect(
checkAngularBuilders({ packageManager, mainConfig: { framework: '@storybook/angular' } })
).resolves.toBeNull();
});
});

describe('Angular < 14.0.0', () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0', '@angular/core': '^12.0.0' },
};
const packageManager = {
getPackageVersion: (packageName) => {
if (packageName === '@angular/core') {
return Promise.resolve('12.0.0');
}

return null;
},
} as Partial<JsPackageManager>;

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
await expect(
checkAngularBuilders({ packageManager, mainConfig: { framework: '@storybook/angular' } })
).resolves.toBeNull();
});
});

describe('Angular >= 14.0.0', () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0', '@angular/core': '^15.0.0' },
};
const packageManager = {
getPackageVersion: (packageName) => {
if (packageName === '@angular/core') {
return Promise.resolve('15.0.0');
}

return null;
},
} as Partial<JsPackageManager>;

describe('has one Storybook builder defined', () => {
beforeEach(() => {
Expand All @@ -89,7 +102,12 @@ describe('is not Nx project', () => {
});

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
await expect(
checkAngularBuilders({
packageManager,
mainConfig: { framework: '@storybook/angular' },
})
).resolves.toBeNull();
});
});

Expand All @@ -106,7 +124,12 @@ describe('is not Nx project', () => {
});

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
await expect(
checkAngularBuilders({
packageManager,
mainConfig: { framework: '@storybook/angular' },
})
).resolves.toBeNull();
});
});

Expand All @@ -124,7 +147,12 @@ describe('is not Nx project', () => {
});

it('should return an empty object', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toMatchObject({});
await expect(
checkAngularBuilders({
packageManager,
mainConfig: { framework: '@storybook/angular' },
})
).resolves.toMatchObject({});
});
});
});
Expand Down
29 changes: 11 additions & 18 deletions code/lib/cli/src/automigrate/fixes/angular-builders-multiproject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import chalk from 'chalk';
import type { Fix } from '../types';
import { isNxProject } from '../../helpers';
import { AngularJSON } from '../../generators/ANGULAR/helpers';
import { getFrameworkPackageName } from '../helpers/mainConfigFile';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface AngularBuildersMultiprojectRunOptions {}
Expand All @@ -12,25 +13,17 @@ export const angularBuildersMultiproject: Fix<AngularBuildersMultiprojectRunOpti
id: 'angular-builders-multiproject',
promptOnly: true,

async check({ packageManager }) {
const packageJSON = await packageManager.retrievePackageJson();

async check({ packageManager, mainConfig }) {
// Skip in case of NX
if (isNxProject(packageJSON)) {
return null;
}
const allDependencies = await packageManager.getAllDependencies();

const angularVersion = allDependencies['@angular/core'];
const angularCoerced = semver.coerce(angularVersion)?.version;

// skip non-angular projects
if (!angularCoerced) {
return null;
}

// Is Angular version lower than 14? -> throw an error (only supports ng 14)
if (semver.lt(angularCoerced, '14.0.0')) {
const angularVersion = await packageManager.getPackageVersion('@angular/core');
const frameworkPackageName = getFrameworkPackageName(mainConfig);

if (
(await isNxProject(packageManager)) ||
frameworkPackageName !== '@storybook/angular' ||
!angularVersion ||
semver.lt(angularVersion, '14.0.0')
) {
return null;
}

Expand Down
96 changes: 64 additions & 32 deletions code/lib/cli/src/automigrate/fixes/angular-builders.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import type { StorybookConfig } from '@storybook/types';
import type { PackageJson } from '../../js-package-manager';
import { makePackageManager, mockStorybookData } from '../helpers/testing-helpers';
import { angularBuilders } from './angular-builders';
import * as helpers from '../../helpers';
import * as angularHelpers from '../../generators/ANGULAR/helpers';
import type { JsPackageManager } from '../../js-package-manager';

const checkAngularBuilders = async ({
packageJson,
main: mainConfig = {},
packageManager,
mainConfig = {},
storybookVersion = '7.0.0',
}: {
packageJson: PackageJson;
main?: Partial<StorybookConfig> & Record<string, unknown>;
packageManager: Partial<JsPackageManager>;
mainConfig?: Partial<StorybookConfig>;
storybookVersion?: string;
}) => {
mockStorybookData({ mainConfig, storybookVersion });

// mock file system (look at eslint plugin test)

return angularBuilders.check({
packageManager: makePackageManager(packageJson),
packageManager: packageManager as any,
storybookVersion,
mainConfig: mainConfig as any,
});
};

Expand All @@ -35,50 +32,70 @@ jest.mock('../../generators/ANGULAR/helpers', () => ({

describe('is Nx project', () => {
beforeEach(() => {
(helpers.isNxProject as any as jest.SpyInstance).mockReturnValue(true);
(helpers.isNxProject as any as jest.SpyInstance).mockResolvedValue(true);
});

it('should return null', async () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0' },
};
const packageManager = {
getPackageVersion: jest.fn().mockImplementation((packageName) => {
if (packageName === '@angular/core') {
return '12.0.0';
}

return null;
}),
} as Partial<JsPackageManager>;

await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
it('should return null', async () => {
await expect(checkAngularBuilders({ packageManager })).resolves.toBeNull();
});
});

describe('is not Nx project', () => {
beforeEach(() => {
(helpers.isNxProject as any as jest.SpyInstance).mockReturnValue(false);
(helpers.isNxProject as any as jest.SpyInstance).mockResolvedValue(false);
});

describe('angular builders', () => {
afterEach(jest.restoreAllMocks);

describe('Angular not found', () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0' },
};
const packageManager = {
getPackageVersion: jest.fn().mockReturnValue(null),
} as Partial<JsPackageManager>;

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
await expect(checkAngularBuilders({ packageManager })).resolves.toBeNull();
});
});

describe('Angular < 14.0.0', () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0', '@angular/core': '^12.0.0' },
};
const packageManager = {
getPackageVersion: (packageName: string) => {
if (packageName === '@angular/core') {
return Promise.resolve('12.0.0');
}

return null;
},
} as Partial<JsPackageManager>;

it('should throw an Error', async () => {
await expect(checkAngularBuilders({ packageJson })).rejects.toThrowErrorMatchingSnapshot();
await expect(
checkAngularBuilders({ packageManager, mainConfig: { framework: '@storybook/angular' } })
).rejects.toThrowErrorMatchingSnapshot();
});
});

describe('Angular >= 14.0.0', () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0', '@angular/core': '^15.0.0' },
};
const packageManager = {
getPackageVersion: (packageName) => {
if (packageName === '@angular/core') {
return Promise.resolve('15.0.0');
}

return null;
},
} as Partial<JsPackageManager>;

describe('has one Storybook builder defined', () => {
beforeEach(() => {
Expand All @@ -89,7 +106,12 @@ describe('is not Nx project', () => {
});

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
await expect(
checkAngularBuilders({
packageManager,
mainConfig: { framework: '@storybook/angular' },
})
).resolves.toBeNull();
});
});

Expand All @@ -107,7 +129,12 @@ describe('is not Nx project', () => {
});

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
await expect(
checkAngularBuilders({
packageManager,
mainConfig: { framework: '@storybook/angular' },
})
).resolves.toBeNull();
});
});

Expand All @@ -124,7 +151,12 @@ describe('is not Nx project', () => {
});

it('should proceed and return data', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toMatchObject({
await expect(
checkAngularBuilders({
packageManager,
mainConfig: { framework: '@storybook/angular' },
})
).resolves.toMatchObject({
mainConfig: {},
packageManager: {},
});
Expand Down
Loading

0 comments on commit 9f43b06

Please sign in to comment.