forked from react-native-community/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: invalid output from config command (react-native-community#1131)
* Moved disabling logger at the top when user used config command * Added e2e tests for config command * Added config command output snapshot * Updated config test snapshot * Added project root replacement helper * Removed default terminal program from snapshots * Moved tests to jest folder and removed additional snapshot * less snapshots; use raw serializer * strip unnecessary parts of the snapshot * Added tmp directory to regex which replaces project root before snapshot * Added slash before passing testFolder for project root replacing helper * Fixed problem with windows paths replacing * CR fixes Co-authored-by: Michał Pierzchała <[email protected]>
- Loading branch information
Showing
8 changed files
with
201 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`shows up current config without unnecessary output 1`] = ` | ||
{ | ||
"root": "<<REPLACED_ROOT>>/TestProject", | ||
"reactNativePath": "<<REPLACED_ROOT>>/TestProject/node_modules/react-native", | ||
"dependencies": {}, | ||
"commands": [ | ||
{ | ||
"name": "log-ios", | ||
"description": "starts iOS device syslog tail" | ||
}, | ||
{ | ||
"name": "run-ios", | ||
"description": "builds your app and starts it on iOS simulator", | ||
"examples": [ | ||
"<<REPLACED>>" | ||
], | ||
"options": [ | ||
"<<REPLACED>>" | ||
] | ||
}, | ||
{ | ||
"name": "log-android", | ||
"description": "starts logkitty" | ||
}, | ||
{ | ||
"name": "run-android", | ||
"description": "builds your app and starts it on a connected Android emulator or device", | ||
"options": [ | ||
"<<REPLACED>>" | ||
] | ||
} | ||
], | ||
"assets": [], | ||
"platforms": { | ||
"ios": {}, | ||
"android": {} | ||
}, | ||
"project": { | ||
"ios": { | ||
"sourceDir": "<<REPLACED_ROOT>>/TestProject/ios", | ||
"folder": "<<REPLACED_ROOT>>/TestProject", | ||
"pbxprojPath": "<<REPLACED_ROOT>>/TestProject/ios/TestProject.xcodeproj/project.pbxproj", | ||
"podfile": "<<REPLACED_ROOT>>/TestProject/ios/Podfile", | ||
"podspecPath": null, | ||
"projectPath": "<<REPLACED_ROOT>>/TestProject/ios/TestProject.xcodeproj", | ||
"projectName": "TestProject.xcodeproj", | ||
"libraryFolder": "Libraries", | ||
"sharedLibraries": [], | ||
"plist": [], | ||
"scriptPhases": [] | ||
}, | ||
"android": { | ||
"sourceDir": "<<REPLACED_ROOT>>/TestProject/android", | ||
"isFlat": true, | ||
"folder": "<<REPLACED_ROOT>>/TestProject", | ||
"stringsPath": "<<REPLACED_ROOT>>/TestProject/android/app/src/main/res/values/strings.xml", | ||
"manifestPath": "<<REPLACED_ROOT>>/TestProject/android/app/src/main/AndroidManifest.xml", | ||
"buildGradlePath": "<<REPLACED_ROOT>>/TestProject/android/build.gradle", | ||
"settingsGradlePath": "<<REPLACED_ROOT>>/TestProject/android/settings.gradle", | ||
"assetsPath": "<<REPLACED_ROOT>>/TestProject/android/app/src/main/assets", | ||
"mainFilePath": "<<REPLACED_ROOT>>/TestProject/android/app/src/main/java/com/testproject/MainApplication.java", | ||
"packageName": "com.testproject", | ||
"packageFolder": "com/testproject", | ||
"appName": "app" | ||
} | ||
} | ||
} | ||
`; |
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,93 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import {wrap} from 'jest-snapshot-serializer-raw'; | ||
import { | ||
runCLI, | ||
getTempDirectory, | ||
cleanup, | ||
writeFiles, | ||
spawnScript, | ||
replaceProjectRootInOutput, | ||
} from '../jest/helpers'; | ||
|
||
const DIR = getTempDirectory('test_root'); | ||
|
||
function isValidJSON(text: string) { | ||
try { | ||
JSON.parse(text); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
// We have to check whether setup_env script fails, if it does then we shouldn't log any info to the console | ||
function createCorruptedSetupEnvScript() { | ||
const originalSetupEnvPath = path.join( | ||
__dirname, | ||
'../packages/cli/setup_env.sh', | ||
); | ||
const originalSetupEnv = fs.readFileSync(originalSetupEnvPath); | ||
const corruptedScript = '#!/bin/sh\n exit 1;'; | ||
fs.writeFileSync(originalSetupEnvPath, corruptedScript); | ||
return () => { | ||
fs.writeFileSync(originalSetupEnvPath, originalSetupEnv); | ||
}; | ||
} | ||
|
||
beforeAll(() => { | ||
// Register all packages to be linked | ||
for (const pkg of ['platform-ios', 'platform-android']) { | ||
spawnScript('yarn', ['link'], { | ||
cwd: path.join(__dirname, `../packages/${pkg}`), | ||
}); | ||
} | ||
|
||
// Clean up folder and re-create a new project | ||
cleanup(DIR); | ||
writeFiles(DIR, {}); | ||
|
||
// Initialise React Native project | ||
|
||
runCLI(DIR, ['init', 'TestProject']); | ||
|
||
// Link CLI to the project | ||
const pkgs = [ | ||
'@react-native-community/cli-platform-ios', | ||
'@react-native-community/cli-platform-android', | ||
]; | ||
|
||
spawnScript('yarn', ['link', ...pkgs], { | ||
cwd: path.join(DIR, 'TestProject'), | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
cleanup(DIR); | ||
}); | ||
|
||
test('shows up current config without unnecessary output', () => { | ||
const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['config']); | ||
const parsedStdout = JSON.parse(stdout); | ||
// Strip unnecessary parts | ||
parsedStdout.commands = parsedStdout.commands.map((command: any) => ({ | ||
...command, | ||
examples: command.examples && ['<<REPLACED>>'], | ||
options: command.options && ['<<REPLACED>>'], | ||
})); | ||
|
||
const configWithReplacedProjectRoots = replaceProjectRootInOutput( | ||
JSON.stringify(parsedStdout, null, 2).replace(/\\\\/g, '\\'), | ||
DIR, | ||
); | ||
expect(wrap(configWithReplacedProjectRoots)).toMatchSnapshot(); | ||
}); | ||
|
||
test('should log only valid JSON config if setting up env throws an error', () => { | ||
const restoreOriginalSetupEnvScript = createCorruptedSetupEnvScript(); | ||
const {stdout, stderr} = runCLI(path.join(DIR, 'TestProject'), ['config']); | ||
|
||
restoreOriginalSetupEnvScript(); | ||
expect(isValidJSON(stdout)).toBe(true); | ||
expect(stderr).toBe(''); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {replaceProjectRootInOutput} from '../helpers'; | ||
|
||
test('should replace project root in output with <<REPLACED_ROOT>> value', () => { | ||
const cwd = '/var/folders/zt/917v0jxx6lg3p_zfh9s_02bm0000gn/T/'; | ||
const output = `{ | ||
"root": "/private${cwd}/test_root/TestProject", | ||
}`; | ||
const outputWithReplacedProjectRoot = `{ | ||
"root": "<<REPLACED_ROOT>>/test_root/TestProject", | ||
}`; | ||
expect(replaceProjectRootInOutput(output, cwd)).toBe( | ||
outputWithReplacedProjectRoot, | ||
); | ||
}); |
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