-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#2758): set exact-o3r-version as default for all schematics if pr…
…ovided when adding @o3r/core
- Loading branch information
1 parent
b5b09dd
commit f86c765
Showing
16 changed files
with
190 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/@o3r/schematics/src/rule-factories/options/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { | ||
callRule, | ||
Rule, | ||
SchematicContext, | ||
Tree, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
lastValueFrom, | ||
} from 'rxjs'; | ||
import { | ||
createSchematicWithOptionsFromWorkspace, | ||
} from './index'; | ||
|
||
let context: SchematicContext; | ||
|
||
describe('createSchematicWithOptionsFromWorkspaceIfInstalled', () => { | ||
beforeEach(() => { | ||
context = { | ||
schematic: { | ||
description: { | ||
collection: { | ||
name: 'MyCollection' | ||
}, | ||
name: 'MySchematic' | ||
} | ||
}, | ||
interactive: false | ||
} as any as SchematicContext; | ||
}); | ||
|
||
it('should call the original schematic with the options', async () => { | ||
const rule = jest.fn((tree: Tree) => tree); | ||
|
||
const originalSchematic = jest.fn((_opts: any): Rule => rule); | ||
const schematic = createSchematicWithOptionsFromWorkspace(originalSchematic); | ||
const options = { | ||
example: 'test' | ||
}; | ||
await lastValueFrom(callRule(schematic(options), Tree.empty(), context)); | ||
expect(originalSchematic).toHaveBeenCalled(); | ||
expect(originalSchematic).toHaveBeenCalledWith(expect.objectContaining(options)); | ||
expect(rule).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call the original schematic with the merge of the options + the options from the angular.json', async () => { | ||
const initialTree = Tree.empty(); | ||
initialTree.create('angular.json', JSON.stringify({ | ||
schematics: { | ||
'*:*': { | ||
commonWithValue: 'default', | ||
workspace: 'workspace', | ||
commonWithUndefined: 'definedValue' | ||
} | ||
} | ||
}, null, 2)); | ||
const rule = jest.fn((tree: Tree) => tree); | ||
|
||
const originalSchematic = jest.fn((_opts: any): Rule => rule); | ||
const schematic = createSchematicWithOptionsFromWorkspace(originalSchematic); | ||
const options: any = { | ||
commonWithValue: 'test', | ||
option: 'option', | ||
commonWithUndefined: undefined | ||
}; | ||
await lastValueFrom(callRule(schematic(options), initialTree, context)); | ||
expect(originalSchematic).toHaveBeenCalled(); | ||
expect(originalSchematic).toHaveBeenCalledWith(expect.objectContaining({ | ||
commonWithValue: 'test', | ||
workspace: 'workspace', | ||
option: 'option', | ||
commonWithUndefined: 'definedValue' | ||
})); | ||
expect(rule).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should works if we chain schematic wrapper', async () => { | ||
const rule = jest.fn((tree: Tree) => tree); | ||
|
||
const originalSchematic = jest.fn((_opts: any): Rule => rule); | ||
const noopSchematicWrapper = (schematicFn: (_opts: any) => Rule) => (opts: any): Rule => schematicFn(opts); | ||
const schematic = noopSchematicWrapper(createSchematicWithOptionsFromWorkspace(originalSchematic)); | ||
const options = { | ||
example: 'test' | ||
}; | ||
await lastValueFrom(callRule(schematic(options), Tree.empty(), context)); | ||
expect(originalSchematic).toHaveBeenCalled(); | ||
expect(originalSchematic).toHaveBeenCalledWith(expect.objectContaining(options)); | ||
expect(rule).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw the original error', async () => { | ||
const error = new Error('error example'); | ||
const rule = jest.fn(() => { | ||
throw error; | ||
}); | ||
|
||
const originalSchematic = jest.fn((_opts: any): Rule => rule); | ||
const schematic = createSchematicWithOptionsFromWorkspace(originalSchematic); | ||
const options = { | ||
example: 'test' | ||
}; | ||
await expect(lastValueFrom(callRule(schematic(options), Tree.empty(), context))).rejects.toThrow(error); | ||
expect(originalSchematic).toHaveBeenCalled(); | ||
expect(originalSchematic).toHaveBeenCalledWith(expect.objectContaining(options)); | ||
expect(rule).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw if the rule is a rejected Promise', async () => { | ||
const rule = jest.fn(() => Promise.reject(new Error('rejected'))); | ||
|
||
const originalSchematic = jest.fn((_opts: any): Rule => rule); | ||
const schematic = createSchematicWithOptionsFromWorkspace(originalSchematic); | ||
const options = { | ||
example: 'test' | ||
}; | ||
await expect(lastValueFrom(callRule(schematic(options), Tree.empty(), context))).rejects.toThrow(); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
packages/@o3r/schematics/src/rule-factories/options/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { | ||
Rule, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
getDefaultOptionsForSchematic, | ||
getWorkspaceConfig, | ||
} from '../../utility'; | ||
|
||
/** | ||
* Factory of the schematic to wrap | ||
* @param options Options of the factory | ||
*/ | ||
type SchematicWrapperFn<S extends Record<string, any>> = (options: S) => Rule; | ||
|
||
/** | ||
* Wrapper method of a schematic to retrieve options from workspace and merge it with the one from the run of the schematic | ||
* @param schematicFn | ||
*/ | ||
export function createSchematicWithOptionsFromWorkspace<S extends Record<string, any>>(schematicFn: SchematicWrapperFn<S>): SchematicWrapperFn<S> { | ||
return (options) => (tree, context) => { | ||
const workspace = getWorkspaceConfig(tree); | ||
const workspaceOptions = getDefaultOptionsForSchematic( | ||
workspace, | ||
context.schematic.description.collection.name, | ||
context.schematic.description.name, | ||
{ projectName: undefined, ...options } | ||
); | ||
const schematicOptionsWithoutUndefined = Object.entries(options).reduce((acc: Record<string, any>, [key, value]) => { | ||
if (typeof value !== 'undefined') { | ||
acc[key] = value; | ||
} | ||
return acc; | ||
}, {}) as S; | ||
const schematicOptions = { | ||
...workspaceOptions, | ||
...schematicOptionsWithoutUndefined | ||
}; | ||
return schematicFn(schematicOptions satisfies S); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.