-
-
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.
Open
Changes from 4 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
180 changes: 180 additions & 0 deletions
180
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,180 @@ | ||
import { createRnvContext, execCLI, getContext, logDebug } from '@rnv/core'; | ||
import semver from 'semver'; | ||
import { _checkAndConfigureTargetSdk, _formatObject, _getCliVersions } from '../taskInfo'; | ||
|
||
jest.mock('@rnv/core'); | ||
jest.mock('semver'); | ||
jest.mock('envinfo', () => ({ | ||
run: jest.fn(), | ||
})); | ||
|
||
beforeEach(() => { | ||
createRnvContext(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('_formatObject tests', () => { | ||
it('should format nested arrays correctly', () => { | ||
// GIVEN | ||
const obj = { | ||
key1: { | ||
key2: { | ||
key3: ['item1', 'item2'], | ||
}, | ||
}, | ||
}; | ||
// WHEN | ||
const result = _formatObject(obj); | ||
// THEN | ||
expect(result).toBe('\nkey1:\n key2:\n key3: item1, item2\n'); | ||
}); | ||
it('should format nested objects correctly', () => { | ||
// GIVEN | ||
const obj = { | ||
key1: { | ||
key2: { | ||
key3: { | ||
version: '1.0.0', | ||
path: '/mocked/path', | ||
}, | ||
}, | ||
}, | ||
}; | ||
// WHEN | ||
const result = _formatObject(obj); | ||
// THEN | ||
expect(result).toBe('\nkey1:\n key2:\n key3: 1.0.0 - /mocked/path\n'); | ||
}); | ||
}); | ||
|
||
describe('_getCliVersions tests', () => { | ||
it('should add CLI versions to parsedInfo', async () => { | ||
// GIVEN | ||
const ctx = getContext(); | ||
const cliVersionOutput = '1.0.0'; | ||
const parsedInfo: { CLI?: any } = {}; | ||
|
||
ctx.cli = { | ||
webosAres: '/path/to/webosAres', | ||
tizen: '/path/to/tizen', | ||
}; | ||
|
||
jest.mocked(execCLI).mockResolvedValue(cliVersionOutput); | ||
semver.coerce = jest.fn().mockReturnValue({ version: '1.0.0' }); | ||
//WHEN | ||
await _getCliVersions(parsedInfo); | ||
//THEN | ||
expect(execCLI).toHaveBeenCalledTimes(2); | ||
expect(parsedInfo).toHaveProperty('CLI'); | ||
expect(parsedInfo.CLI).toHaveProperty('WEBOS CLI', { version: '1.0.0', path: '/path/to/webosAres' }); | ||
expect(parsedInfo.CLI).toHaveProperty('TIZEN CLI', { version: '1.0.0', path: '/path/to/tizen' }); | ||
}); | ||
it('should not add CLI versions if no cli is present in context', async () => { | ||
// GIVEN | ||
const parsedInfo: { CLI?: any } = {}; | ||
//WHEN | ||
await _getCliVersions(parsedInfo); | ||
//THEN | ||
expect(execCLI).not.toHaveBeenCalled(); | ||
expect(parsedInfo.CLI).toBeUndefined(); | ||
}); | ||
it('should add only available CLI versions if one cli is present in context', async () => { | ||
// GIVEN | ||
const ctx = getContext(); | ||
const cliVersionOutput = '1.0.0'; | ||
const parsedInfo: { CLI?: any } = {}; | ||
|
||
ctx.cli = { | ||
webosAres: '/path/to/webosAres', | ||
}; | ||
jest.mocked(execCLI).mockResolvedValue(cliVersionOutput); | ||
semver.coerce = jest.fn().mockReturnValue({ version: '1.0.0' }); | ||
//WHEN | ||
await _getCliVersions(parsedInfo); | ||
//THEN | ||
expect(execCLI).toHaveBeenCalledTimes(1); | ||
expect(parsedInfo).toHaveProperty('CLI'); | ||
expect(parsedInfo.CLI).toHaveProperty('WEBOS CLI', { version: '1.0.0', path: '/path/to/webosAres' }); | ||
expect(parsedInfo.CLI).not.toHaveProperty('TIZEN CLI'); | ||
}); | ||
|
||
it('should handle errors properly', async () => { | ||
// GIVEN | ||
const ctx = getContext(); | ||
const parsedInfo: { CLI?: any } = {}; | ||
ctx.cli = { | ||
webosAres: '/path/to/webosAres', | ||
tizen: '/path/to/tizen', | ||
}; | ||
|
||
const error = new Error('Test error'); | ||
|
||
jest.mocked(execCLI).mockRejectedValue(error); | ||
semver.coerce = jest.fn().mockReturnValue(null); | ||
|
||
//WHEN | ||
await _getCliVersions(parsedInfo); | ||
//THEN | ||
expect(execCLI).toHaveBeenCalledTimes(2); | ||
expect(parsedInfo.CLI).toBeUndefined(); | ||
expect(logDebug).toHaveBeenCalledTimes(2); | ||
expect(logDebug).toHaveBeenLastCalledWith(`Error getting version for TIZEN CLI: `, error); | ||
}); | ||
}); | ||
|
||
describe('_checkAndConfigureTargetSdk tests', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
// GIVEN | ||
|
||
it('should call the SDK module function', async () => { | ||
// GIVEN | ||
const mockConfigureFunction = jest.fn(); | ||
jest.mock( | ||
`@rnv/mockModule`, | ||
() => ({ | ||
mockConfigureFunction, | ||
}), | ||
{ virtual: true } | ||
); | ||
// WHEN | ||
await _checkAndConfigureTargetSdk('mockModule', 'mockConfigureFunction'); | ||
|
||
//THEN | ||
expect(mockConfigureFunction).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle error and log it if module is not found', async () => { | ||
// GIVEN | ||
const moduleName = 'nonExistentModule'; | ||
const configureFunction = 'checkAndConfigureMockSdks'; | ||
|
||
//WHEN | ||
await _checkAndConfigureTargetSdk(moduleName, configureFunction); | ||
|
||
// THEN | ||
expect(logDebug).toHaveBeenCalledTimes(1); | ||
expect(logDebug).toHaveBeenCalledWith( | ||
`Error configuring ${moduleName} SDK: `, | ||
expect.objectContaining({ | ||
message: expect.stringContaining(`Cannot find module '@rnv/${moduleName}'`), | ||
}) | ||
); | ||
}); | ||
it('should handle error and log it if the configure function throws an error', async () => { | ||
jest.mock( | ||
'@rnv/mockModule', | ||
() => ({ | ||
mockConfigureFunction: jest.fn().mockRejectedValue(new Error('Configuration error')), | ||
}), | ||
{ virtual: true } | ||
); | ||
await _checkAndConfigureTargetSdk('mockModule', 'mockConfigureFunction'); | ||
|
||
expect(logDebug).toHaveBeenCalledWith('Error configuring mockModule SDK: ', new Error('Configuration error')); | ||
}); | ||
}); |
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,101 @@ | ||
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); | ||
return logToSummary(_formatObject(parsedInfo)); | ||
}, | ||
task: RnvTaskName.info, | ||
isGlobalScope: true, | ||
}); | ||
|
||
export 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); | ||
} | ||
}; | ||
|
||
export 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); | ||
} | ||
}; | ||
|
||
export 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; | ||
} | ||
}; | ||
export 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