-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
added 'info' task using envinfo #1632
Open
ElenaDiachenko
wants to merge
6
commits into
main
Choose a base branch
from
add_envinfo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+323
−19
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
09c1798
added 'info env' task using envinfo
ElenaDiachenko fc2f83f
fix
ElenaDiachenko f9aa3f0
add UT
ElenaDiachenko 823ef50
remove 'info status' task
ElenaDiachenko e5fe7a6
fixed UTs
ElenaDiachenko 8adb036
removed unnecessary export
ElenaDiachenko 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
179 changes: 179 additions & 0 deletions
179
packages/engine-core/src/tasks/global/__tests__/taskInfo.test.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,179 @@ | ||
import { createRnvContext, execCLI, getContext, logDebug, logToSummary, logError } from '@rnv/core'; | ||
import envinfo from 'envinfo'; | ||
import semver from 'semver'; | ||
import taskInfo from '../taskInfo'; | ||
|
||
jest.mock('@rnv/core'); | ||
jest.mock('semver'); | ||
jest.mock('envinfo', () => ({ | ||
run: jest.fn(), | ||
})); | ||
|
||
beforeEach(() => { | ||
createRnvContext(); | ||
jest.resetAllMocks(); | ||
jest.resetModules(); | ||
}); | ||
|
||
describe('taskInfo tests', () => { | ||
it('Execute task.rnv.info', async () => { | ||
// GIVEN | ||
const ctx = getContext(); | ||
ctx.cli = { | ||
webosAres: '/path/to/webosAres', | ||
tizen: '/path/to/tizen', | ||
}; | ||
const mockEnvInfo = { System: { OS: 'macOS', CPU: 'Intel' } }; | ||
jest.mocked(envinfo.run).mockResolvedValue(JSON.stringify(mockEnvInfo)); | ||
|
||
const mockCheckAndConfigure = jest.fn(); | ||
jest.mock( | ||
'@rnv/sdk-tizen', | ||
() => ({ | ||
checkAndConfigureTizenSdks: mockCheckAndConfigure, | ||
}), | ||
{ virtual: true } | ||
); | ||
jest.mock( | ||
'@rnv/sdk-webos', | ||
() => ({ | ||
checkAndConfigureWebosSdks: mockCheckAndConfigure, | ||
}), | ||
{ virtual: true } | ||
); | ||
|
||
const mockExecCLI = jest.mocked(execCLI).mockResolvedValue('1.0.0'); | ||
semver.coerce = jest.fn().mockReturnValue({ version: '1.0.0' }); | ||
|
||
// WHEN | ||
await expect( | ||
taskInfo.fn?.({ | ||
ctx, | ||
taskName: 'MOCK_taskName', | ||
originTaskName: 'MOCK_originTaskName', | ||
parentTaskName: 'MOCK_parentTaskName', | ||
shouldSkip: false, | ||
}) | ||
).resolves.toEqual(true); | ||
// THEN | ||
expect(envinfo.run).toHaveBeenCalled(); | ||
expect(mockCheckAndConfigure).toHaveBeenCalledTimes(2); | ||
expect(mockExecCLI).toHaveBeenCalledTimes(2); | ||
expect(logToSummary).toHaveBeenCalledWith(expect.stringContaining('System:\n OS: macOS\n CPU: Intel\n')); | ||
expect(logToSummary).toHaveBeenCalledWith(expect.stringContaining('WEBOS CLI: 1.0.0 - /path/to/webosAres\n')); | ||
expect(logToSummary).toHaveBeenCalledWith(expect.stringContaining('TIZEN CLI: 1.0.0 - /path/to/tizen\n')); | ||
}); | ||
it('should handle errors when getting environment info', async () => { | ||
// GIVEN | ||
const ctx = getContext(); | ||
const mockError = new Error('Test error'); | ||
jest.mocked(envinfo.run).mockRejectedValue(mockError); | ||
//WHEN | ||
await expect( | ||
taskInfo.fn?.({ | ||
ctx, | ||
taskName: 'MOCK_taskName', | ||
originTaskName: 'MOCK_originTaskName', | ||
parentTaskName: 'MOCK_parentTaskName', | ||
shouldSkip: false, | ||
}) | ||
).resolves.toEqual(true); | ||
//THEN | ||
expect(logError).toHaveBeenCalledWith(mockError); | ||
expect(logToSummary).toHaveBeenCalledWith(expect.objectContaining(mockError)); | ||
}); | ||
it('should handle errors during SDK configuration', async () => { | ||
// GIVEN | ||
const ctx = getContext(); | ||
const mockEnvInfo = { System: { OS: 'macOS', CPU: 'Intel' } }; | ||
jest.mocked(envinfo.run).mockResolvedValue(JSON.stringify(mockEnvInfo)); | ||
const cliVersionOutput = '1.0.0'; | ||
ctx.cli = { | ||
webosAres: '/path/to/webosAres', | ||
tizen: '/path/to/tizen', | ||
}; | ||
jest.mocked(execCLI).mockResolvedValue(cliVersionOutput); | ||
semver.coerce = jest.fn().mockReturnValue({ version: '1.0.0' }); | ||
jest.mock( | ||
'@rnv/sdk-tizen', | ||
() => ({ | ||
checkAndConfigureTizenSdks: jest.fn().mockRejectedValue(new Error('Tizen SDK error')), | ||
}), | ||
{ virtual: true } | ||
); | ||
jest.mock( | ||
'@rnv/sdk-webos', | ||
() => ({ | ||
checkAndConfigureWebosSdks: jest.fn(), | ||
}), | ||
{ virtual: true } | ||
); | ||
|
||
// WHEN | ||
await expect( | ||
taskInfo.fn?.({ | ||
ctx, | ||
taskName: 'MOCK_taskName', | ||
originTaskName: 'MOCK_originTaskName', | ||
parentTaskName: 'MOCK_parentTaskName', | ||
shouldSkip: false, | ||
}) | ||
).resolves.toEqual(true); | ||
// THEN | ||
expect(envinfo.run).toHaveBeenCalled(); | ||
expect(execCLI).toHaveBeenCalledTimes(2); | ||
expect(logDebug).toHaveBeenCalledWith('Error configuring sdk-tizen SDK: ', new Error('Tizen SDK error')); | ||
expect(logDebug).not.toHaveBeenCalledWith('Error configuring sdk-webos SDK: ', new Error('WebOS SDK error')); | ||
expect(logToSummary).toHaveBeenCalledWith(expect.stringContaining('System:\n OS: macOS\n CPU: Intel\n')); | ||
expect(logToSummary).toHaveBeenCalledWith(expect.stringContaining('WEBOS CLI: 1.0.0 - /path/to/webosAres\n')); | ||
}); | ||
it('should handle error and log it if module is not found', async () => { | ||
// GIVEN | ||
const ctx = getContext(); | ||
const mockEnvInfo = { System: { OS: 'macOS', CPU: 'Intel' } }; | ||
jest.mocked(envinfo.run).mockResolvedValue(JSON.stringify(mockEnvInfo)); | ||
const cliVersionOutput = '1.0.0'; | ||
const errorMessage = `Cannot find module '@rnv/sdk-tizen'`; | ||
ctx.cli = { | ||
webosAres: '/path/to/webosAres', | ||
}; | ||
jest.mocked(execCLI).mockResolvedValue(cliVersionOutput); | ||
semver.coerce = jest.fn().mockReturnValue({ version: '1.0.0' }); | ||
jest.mock( | ||
'@rnv/sdk-tizen', | ||
() => { | ||
throw new Error(errorMessage); | ||
}, | ||
{ virtual: true } | ||
); | ||
jest.mock( | ||
'@rnv/sdk-webos', | ||
() => ({ | ||
checkAndConfigureWebosSdks: jest.fn(), | ||
}), | ||
{ virtual: true } | ||
); | ||
|
||
// WHEN | ||
await expect( | ||
taskInfo.fn?.({ | ||
ctx, | ||
taskName: 'MOCK_taskName', | ||
originTaskName: 'MOCK_originTaskName', | ||
parentTaskName: 'MOCK_parentTaskName', | ||
shouldSkip: false, | ||
}) | ||
).resolves.toEqual(true); | ||
// THEN | ||
expect(envinfo.run).toHaveBeenCalled(); | ||
expect(execCLI).toHaveBeenCalledTimes(1); | ||
expect(logDebug).toHaveBeenCalledWith( | ||
`Error configuring sdk-tizen SDK: `, | ||
expect.objectContaining({ | ||
message: expect.stringContaining(errorMessage), | ||
}) | ||
); | ||
expect(logToSummary).toHaveBeenCalledWith(expect.stringContaining('System:\n OS: macOS\n CPU: Intel\n')); | ||
expect(logToSummary).toHaveBeenCalledWith(expect.stringContaining('WEBOS CLI: 1.0.0 - /path/to/webosAres\n')); | ||
}); | ||
}); |
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,102 @@ | ||
import { createTask, RnvTaskName, getContext, execCLI, logError, logDebug, logToSummary } from '@rnv/core'; | ||
import envinfo from 'envinfo'; | ||
import semver from 'semver'; | ||
|
||
export default createTask({ | ||
description: 'Get relevant version info about OS, toolchain and libraries', | ||
fn: async () => { | ||
const parsedInfo = await _getEnvironmentInfo(); | ||
await _checkAndConfigureSdks(); | ||
await _getCliVersions(parsedInfo); | ||
logToSummary(_formatObject(parsedInfo)); | ||
return true; | ||
}, | ||
task: RnvTaskName.info, | ||
isGlobalScope: true, | ||
}); | ||
|
||
const _checkAndConfigureSdks = async () => { | ||
const moduleConfigs = [ | ||
{ moduleName: 'sdk-tizen', configureFunction: 'checkAndConfigureTizenSdks' }, | ||
{ moduleName: 'sdk-webos', configureFunction: 'checkAndConfigureWebosSdks' }, | ||
]; | ||
for (const config of moduleConfigs) { | ||
const { moduleName, configureFunction } = config; | ||
await _checkAndConfigureTargetSdk(moduleName, configureFunction); | ||
} | ||
}; | ||
|
||
const _checkAndConfigureTargetSdk = async (moduleName: string, configureFunction: string): Promise<void> => { | ||
try { | ||
const SDKModule = require(`@rnv/${moduleName}`); | ||
await SDKModule[configureFunction](); | ||
} catch (e) { | ||
logDebug(`Error configuring ${moduleName} SDK: `, e); | ||
} | ||
}; | ||
|
||
const _getCliVersions = async (parsedInfo: any) => { | ||
const c = getContext(); | ||
const cliVersions: { [key: string]: { version: string; path: string } } = {}; | ||
|
||
const addCliVersion = async (cli: string, command: string, path: string, cliName: string) => { | ||
try { | ||
const cliVersionOutput = await execCLI(cli, command); | ||
const cliVersionNumber = semver.coerce(cliVersionOutput)?.version; | ||
if (cliVersionNumber) { | ||
cliVersions[`${cliName.replace('-', ' ').toUpperCase()}`] = { version: cliVersionNumber, path }; | ||
} | ||
} catch (e) { | ||
logDebug(`Error getting version for ${cliName}: `, e); | ||
} | ||
}; | ||
|
||
if (c.cli.webosAres) { | ||
await addCliVersion('webosAres', '--version', c.cli.webosAres, 'WEBOS CLI'); | ||
} | ||
if (c.cli.tizen) { | ||
await addCliVersion('tizen', 'version', c.cli.tizen, 'TIZEN CLI'); | ||
} | ||
if (Object.keys(cliVersions).length) { | ||
parsedInfo.CLI = cliVersions; | ||
} | ||
}; | ||
const _formatObject = (obj: any, indent = 0) => { | ||
let formattedString = ''; | ||
if (indent === 0) formattedString += '\n'; | ||
for (const key in obj) { | ||
if (obj[key] && typeof obj[key] === 'object' && obj[key].version) { | ||
formattedString += | ||
' '.repeat(indent) + `${key}: ${obj[key].version} ${obj[key].path ? `- ${obj[key].path}` : ''}\n`; | ||
} else if (Array.isArray(obj[key])) { | ||
formattedString += ' '.repeat(indent) + `${key}: ${obj[key].join(', ')}\n`; | ||
} else if (typeof obj[key] === 'object') { | ||
formattedString += ' '.repeat(indent) + `${key}:\n`; | ||
formattedString += _formatObject(obj[key], indent + 2); | ||
} else { | ||
formattedString += ' '.repeat(indent) + `${key}: ${obj[key]}\n`; | ||
} | ||
} | ||
return formattedString; | ||
}; | ||
|
||
const _getEnvironmentInfo = async () => { | ||
try { | ||
const output = await envinfo.run( | ||
{ | ||
System: ['OS', 'CPU', 'Memory', 'Shell'], | ||
Binaries: ['Node', 'Yarn', 'npm', 'Watchman'], | ||
Managers: ['CocoaPods'], | ||
Languages: ['Ruby', 'Java'], | ||
IDEs: ['Xcode', 'Android Studio'], | ||
SDKs: ['iOS SDK', 'Android SDK'], | ||
npmPackages: ['react', 'react-native', '@react-native-community/cli', 'rnv'], | ||
npmGlobalPackages: ['*react-native*', 'rnv', 'lerna'], | ||
}, | ||
{ json: true, duplicates: false } | ||
); | ||
return JSON.parse(output); | ||
} catch (e) { | ||
logError(e); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
@ElenaDiachenko consider changing the scope of tests.
instead of testing what could be considered an internal structure of task:
import task itself
and create test case scenarios around it.
that way what is tested (taskInfo) is treated as blackbox because nobody will use
_checkAndConfigureTargetSdk, _formatObject, _getCliVersions
directly (those should be non-expoted methods internal to task).the method you want to test in this case would be
taskClean.fn()
because that's the public api called by anybody who uses this taskexample:
https://github.com/flexn-io/renative/blob/main/packages/engine-core/src/tasks/global/__tests__/taskClean.test.ts#L35