Skip to content

Commit

Permalink
change policy format for env.jsonc (#6780)
Browse files Browse the repository at this point in the history
  • Loading branch information
GiladShoham authored and luvkapur committed Dec 14, 2022
1 parent 7fe4507 commit 4e1eed1
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 75 deletions.
102 changes: 50 additions & 52 deletions e2e/harmony/dependencies/env-jsonc-policies.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ const ENV_POLICY = {
name: '@types/react',
version: '18.0.25',
hidden: true,
force: true,
force: true
},
{
name: '@types/react-dom',
version: '^18.0.0',
hidden: true,
force: true,
force: true
},
{
name: '@types/jest',
version: '29.2.2',
hidden: true,
force: true,
force: true
},
],
runtime: [
Expand All @@ -54,10 +54,10 @@ const ENV_POLICY = {
{
name: 'is-odd',
version: '3.0.1',
force: true,
force: true
},
],
};
]
}

describe('env-jsonc-policies', function () {
this.timeout(0);
Expand All @@ -70,7 +70,7 @@ describe('env-jsonc-policies', function () {
envId = `${helper.scopes.remote}/react-based-env`;
helper.command.create('react', 'button', '-p button');
helper.fs.prependFile('button/button.tsx', 'import isPositive from "is-positive";\n');
helper.env.setCustomNewEnv(undefined, undefined, { policy: ENV_POLICY });
helper.env.setCustomNewEnv(undefined, undefined, {policy: ENV_POLICY});
helper.command.setEnv('button', envId);
helper.command.install();
componentShowParsed = helper.command.showComponentParsed('button');
Expand All @@ -85,85 +85,81 @@ describe('env-jsonc-policies', function () {
envShowParsed = helper.command.showComponentParsed('react-based-env');
});
it('should add peers as runtime deps of the env', () => {
expect(envShowParsed.packageDependencies).to.include({ react: '^18.0.0' });
expect(envShowParsed.packageDependencies).to.include({ 'react-dom': '^18.0.0' });
expect(envShowParsed.packageDependencies).to.include({ graphql: '14.7.0' });
expect(envShowParsed.packageDependencies).to.include({react : '^18.0.0'});
expect(envShowParsed.packageDependencies).to.include({'react-dom' : '^18.0.0'});
expect(envShowParsed.packageDependencies).to.include({graphql : '14.7.0'});
});
// TODO: implement if needed
// it('should add devs as runtime / dev deps of the env', () => {
// });
});
})
describe('affect component', () => {
describe('peers effect', () => {
it('should take supported range from env.jsonc for used peers', () => {
expect(componentShowParsed.peerPackageDependencies).to.include({ react: '^17.0.0 || ^18.0.0' });
expect(componentShowParsed.peerPackageDependencies).to.include({react : "^17.0.0 || ^18.0.0"});
});
it('should not add unused peer deps from env.jsonc', () => {
const keys = Object.keys(componentShowParsed.peerPackageDependencies);
// Validate we use the correct array
expect(keys).to.be.not.empty;
expect(keys).to.not.include('graphql');
});
});
})
})
describe('devs effect', () => {
describe('used deps', () => {
it('should remove hidden devs deps configured by env.jsonc', () => {
const newDeps = helper.command.showComponentParsedHarmonyByTitle('button', 'dependencies');
expect(newDeps).to.not.be.empty;
const typesReactEntry = newDeps.find((dep) => dep.id === '@types/react');
const typesReactEntry = newDeps.find(dep => dep.id === '@types/react');
expect(typesReactEntry).to.be.undefined;
});
})

it('should save used dep as hidden in the deps resolver deps', () => {
const depResolverAspectEntry = helper.command.showAspectConfig(
'button',
'teambit.dependencies/dependency-resolver'
);
const typesReactEntry = depResolverAspectEntry.data.dependencies.find((dep) => dep.id === '@types/react');
expect(typesReactEntry).to.include({ version: '18.0.25' });
expect(typesReactEntry).to.include({ hidden: true });
});
const depResolverAspectEntry = helper.command.showAspectConfig('button', 'teambit.dependencies/dependency-resolver');
const typesReactEntry = depResolverAspectEntry.data.dependencies.find(dep => dep.id === '@types/react');
expect(typesReactEntry).to.include({'version' : "18.0.25"});
expect(typesReactEntry).to.include({'hidden' : true});
})

it('should have used dev deps with the version configured in the env.jsonc in the legacy dev deps', () => {
expect(componentShowParsed.devPackageDependencies).to.include({ '@types/react': '18.0.25' });
});
expect(componentShowParsed.devPackageDependencies).to.include({'@types/react' : "18.0.25"});
})

it('should install used dev deps specified by the env dev deps', () => {
const typesReactVersion = fs.readJsonSync(
const typesReactVersion =
fs.readJsonSync(
resolveFrom(path.join(helper.fixtures.scopes.localPath, 'button'), ['@types/react/package.json'])
).version;
).version
expect(typesReactVersion).to.eq('18.0.25');
});
});
})
})
describe('not used deps', () => {
it('should save forced unused dep as hidden in the deps resolver deps', () => {
const depResolverAspectEntry = helper.command.showAspectConfig(
'button',
'teambit.dependencies/dependency-resolver'
);
const typesJestEntry = depResolverAspectEntry.data.dependencies.find((dep) => dep.id === '@types/jest');
expect(typesJestEntry).to.include({ version: '29.2.2' });
expect(typesJestEntry).to.include({ hidden: true });
});
const depResolverAspectEntry = helper.command.showAspectConfig('button', 'teambit.dependencies/dependency-resolver');
const typesJestEntry = depResolverAspectEntry.data.dependencies.find(dep => dep.id === '@types/jest');
expect(typesJestEntry).to.include({'version' : "29.2.2"});
expect(typesJestEntry).to.include({'hidden' : true});
})

it('should have forced unused dev deps with the version configured in the env.jsonc in the legacy dev deps', () => {
expect(componentShowParsed.devPackageDependencies).to.include({ '@types/jest': '29.2.2' });
});
expect(componentShowParsed.devPackageDependencies).to.include({'@types/jest' : "29.2.2"});
})

it('should install forced unused dev deps specified by the env dev deps', () => {
const typesJestVersion = fs.readJsonSync(
const typesJestVersion =
fs.readJsonSync(
resolveFrom(path.join(helper.fixtures.scopes.localPath, 'button'), ['@types/jest/package.json'])
).version;
).version
expect(typesJestVersion).to.eq('29.2.2');
});
});
});
});
})
})
})
})
describe('runtime effect', () => {
describe('not forced', () => {
describe('detected (used) deps', () => {
it('should take version from env.jsonc for used deps without force', () => {
expect(componentShowParsed.packageDependencies).to.include({ 'is-positive': '2.0.0' });
expect(componentShowParsed.packageDependencies).to.include({'is-positive' : "2.0.0"});
});
it('should install is-positive specified by the env', () => {
expect(
Expand All @@ -183,14 +179,16 @@ describe('env-jsonc-policies', function () {
});
describe('forced', () => {
it('should take version from env.jsonc for unused deps', () => {
expect(componentShowParsed.packageDependencies).to.include({ 'is-odd': '3.0.1' });
expect(componentShowParsed.packageDependencies).to.include({'is-odd' : "3.0.1"});
});
it('should install is-odd specified by the env', () => {
expect(
fs.readJsonSync(resolveFrom(path.join(helper.fixtures.scopes.localPath, 'button'), ['is-odd/package.json']))
.version
fs.readJsonSync(
resolveFrom(path.join(helper.fixtures.scopes.localPath, 'button'), ['is-odd/package.json'])
).version
).to.eq('3.0.1');
});
});
});
})
})

});
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ export class DependencyInstaller {
const manifest = workspaceManifest.componentsManifestsMap.get(packageName);
if (manifest) {
acc[dir] = manifest.toJson({ copyPeerToRuntime: copyPeerToRuntimeOnComponents });
acc[dir].defaultPeerDependencies = fromPairs(manifest.envPolicy.selfPolicy.toNameVersionTuple());
acc[dir].defaultPeerDependencies = fromPairs(
manifest.envPolicy.selfPolicy.toNameVersionTuple()
);
}
return acc;
}, {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ describe('validateEnvPolicyConfigObject', () => {
);
});
it('should throw an exception if peer supportedRange is null', () => {
expect(() =>
validateEnvPolicyConfigObject({ peers: [{ name: 'peer', supportedRange: null }] } as any)
).toThrowError('Peer "peer" has no supportedRange set');
expect(() => validateEnvPolicyConfigObject({ peers: [{ name: 'peer', supportedRange: null }] } as any)).toThrowError(
'Peer "peer" has no supportedRange set'
);
});
it('should throw an exception if peer version is empty', () => {
expect(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function validateEnvPolicyConfigObject(configObject: EnvPolicyConfigObjec
}
}

function validateEnvPeers(peers: EnvJsoncPolicyPeerEntry[]) {
function validateEnvPeers(peers: EnvJsoncPolicyPeerEntry[]){
for (const peer of peers) {
if (peer.supportedRange === '') {
throw new BitError(`Peer "${peer.name}" has an empty supportedRange`);
Expand Down
6 changes: 5 additions & 1 deletion scopes/dependencies/dependency-resolver/policy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export {
AddEntryOptions as WorkspacePolicyAddEntryOptions,
WorkspacePolicyEntry,
} from './workspace-policy';
export { VariantPolicy, VariantPolicyConfigObject, SerializedVariantPolicy } from './variant-policy';
export {
VariantPolicy,
VariantPolicyConfigObject,
SerializedVariantPolicy,
} from './variant-policy';

export { EnvPolicy, EnvPolicyConfigObject } from './env-policy';
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
export {
VariantPolicy,
VariantPolicyConfigObject,
SerializedVariantPolicy,
VariantPolicyEntry,
createVariantPolicyEntry,
DependencySource,
} from './variant-policy';
export { VariantPolicy, VariantPolicyConfigObject, SerializedVariantPolicy, VariantPolicyEntry, createVariantPolicyEntry, DependencySource } from './variant-policy';
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ export class VariantPolicy implements Policy<VariantPolicyConfigObject> {
return res;
}

static fromConfigObject(configObject, source?: DependencySource, hidden?: boolean, force = true): VariantPolicy {
static fromConfigObject(
configObject,
source?: DependencySource,
hidden?: boolean,
force = true
): VariantPolicy {
const runtimeEntries = entriesFromKey(configObject, 'dependencies', source, hidden, force);
const devEntries = entriesFromKey(configObject, 'devDependencies', source, hidden, force);
const peerEntries = entriesFromKey(configObject, 'peerDependencies', source, hidden, force);
Expand Down
6 changes: 1 addition & 5 deletions src/e2e-helper/e2e-env-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,7 @@ export default class EnvHelper {
* @param extensionsBaseFolder
* @returns
*/
setCustomNewEnv(
extensionsBaseFolder = 'react-based-env',
basePackages: string[] = ['@teambit/react.react-env'],
envJsoncOptions: GenerateEnvJsoncOptions
): string {
setCustomNewEnv(extensionsBaseFolder = 'react-based-env', basePackages: string[] = ['@teambit/react.react-env'], envJsoncOptions: GenerateEnvJsoncOptions): string {
this.fixtures.copyFixtureExtensions(extensionsBaseFolder);
this.command.addComponent(extensionsBaseFolder);
this.fixtures.generateEnvJsoncFile(extensionsBaseFolder, envJsoncOptions);
Expand Down
6 changes: 3 additions & 3 deletions src/e2e-helper/e2e-fixtures-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ export default () => 'comp${index} and ' + ${nextComp}();`;
};
const envJsoncFileContentJson = {
policy: options.policy || {},
patterns: options.patterns || defaultPatterns,
};
this.fs.outputFile(envJsoncFile, JSON.stringify(envJsoncFileContentJson, null, 2));
patterns: options.patterns || defaultPatterns
}
this.fs.outputFile(envJsoncFile, JSON.stringify(envJsoncFileContentJson, null, 2))
}

populateEnvMainRuntime(
Expand Down

0 comments on commit 4e1eed1

Please sign in to comment.