-
Notifications
You must be signed in to change notification settings - Fork 41
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
fix(#2758): set exact-o3r-version as default for all schematics if provided when adding @o3r/core #2769
Merged
Merged
fix(#2758): set exact-o3r-version as default for all schematics if provided when adding @o3r/core #2769
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed it in the team and half of them preferred the
.reduce()
because we are not used toObject.fromEntries()