Skip to content
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 tests for Doctor #677

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/cli/src/lib/cmds/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ async function androidSdkDoctor(config: Config, log: Log): Promise<boolean> {
return true;
}

export async function doctor(log: Log = new ConsoleLog('doctor')): Promise<boolean> {
const config = await loadOrCreateConfig(log);
export async function doctor(
log: Log = new ConsoleLog('doctor'), configPath?: string | undefined): Promise<boolean> {
const config = await loadOrCreateConfig(log, undefined, configPath);
const jdkResult = await jdkDoctor(config, log);
const androidSdkResult = await androidSdkDoctor(config, log);
if (jdkResult && androidSdkResult) {
Expand Down
90 changes: 18 additions & 72 deletions packages/cli/src/spec/DoctorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,29 @@
* limitations under the License.
*/

import {loadOrCreateConfig} from '../lib/config';
import * as mock from 'mock-fs';
import {updateConfig} from '../lib/cmds/updateConfig';
import {MockLog} from '@bubblewrap/core';
import minimist = require('minimist');
import {doctor} from '../lib/cmds/doctor';
import {MockPrompt} from './mock/MockPrompt';
import {enUS as messages} from '../lib/strings';

describe('doctor', () => {
let originalProcess: NodeJS.Process;

beforeEach(() => {
// Set process to a linux platform so we can run the test against expected paths on MacOS and
// Windows.
process = {
platform: 'linux',
env: {
'PATH': '',
},
} as unknown as NodeJS.Process;
});

afterEach(() => {
// Resets to the original process to avoid breaking other tests.
process = originalProcess;
});

describe('#jdkDoctor', () => {
it('checks that the expected error message is sent in case that the path given isn\'t' +
' valid', async () => {
// Creates a mock file system.
mock({
'old/path/to/jdk': {
'release': 'JAVA_VERSION="1.8.3',
},
'old/path/to/sdk': {
'path/to/sdk': {
'tools': {},
},
'new/path/to/jdk': {},
'path/to/config': '{"jdkPath":"path/to/jdk","androidSdkPath":"path/to/sdk"}',
});

const mockLog = new MockLog();
const mockPrompt = new MockPrompt();
// Since 'createConfig' will be called, we push 3 future answers to 'mockPrompt'.
mockPrompt.addMessage('false'); // Should bubblewrap download the JDK?
mockPrompt.addMessage('old/path/to/jdk'); // The path of the jdk.
mockPrompt.addMessage('old/path/to/sdk'); // The path of the androidSdk.
// Create config file.
await loadOrCreateConfig(mockLog, mockPrompt);
expect(await doctor(mockLog)).toBeTrue();
// Change the jdkPath of the config file to a path which isn't a valid jdkPath.
const parsedMockArgs = minimist(['--jdkPath', 'new/path/to/jdk']);
await updateConfig(parsedMockArgs, mockLog);
expect(await doctor(mockLog)).toBeFalse();
await expectAsync(doctor(mockLog, 'path/to/config')).toBeResolvedTo(false);
// Check that the correct message was sent.
const logErrors: Array<string> = mockLog.getReceivedData();
expect(logErrors[logErrors.length - 1]).toEqual(messages.jdkPathIsNotCorrect);
const lastMessage = logErrors[logErrors.length - 1];
expect(lastMessage.indexOf('jdkPath isn\'t correct')).toBeGreaterThanOrEqual(0);
mock.restore();
});

Expand All @@ -84,25 +50,15 @@ describe('doctor', () => {
'path/to/sdk': {
'tools': {},
},
'new/path/to/jdk': {
'release': 'JAVA_VERSION="1.7',
},
'path/to/config': '{"jdkPath":"path/to/jdk","androidSdkPath":"path/to/sdk"}',
});

const mockLog = new MockLog();
const mockPrompt = new MockPrompt();
// Since 'createConfig' will be called, we push 3 future answers to 'mockPrompt'.
mockPrompt.addMessage('false'); // Should bubblewrap download the JDK?
mockPrompt.addMessage('path/to/jdk'); // The path of the jdk.
mockPrompt.addMessage('path/to/sdk'); // The path of the androidSdk.
// Create config file.
await loadOrCreateConfig(mockLog, mockPrompt);
// Change the jdkPath of the config file to a path which isn't a supported jdkPath.
const parsedMockArgs = minimist(['--jdkPath', 'new/path/to/jdk']);
await updateConfig(parsedMockArgs, mockLog);
expect(await doctor(mockLog)).toBeFalse();
await expectAsync(doctor(mockLog, 'path/to/config')).toBeResolvedTo(false);
// Check that the correct message was sent.
const logErrors: Array<string> = mockLog.getReceivedData();
expect(logErrors[logErrors.length - 1]).toEqual(messages.jdkIsNotSupported);
const lastMessage = logErrors[logErrors.length - 1];
expect(lastMessage.indexOf('Unsupported jdk version')).toBeGreaterThanOrEqual(0);
mock.restore();
});
});
Expand All @@ -112,26 +68,16 @@ describe('doctor', () => {
' valid', async () => {
// Creates a mock file system.
mock({
'old/path/to/jdk': {
'release': 'JAVA_VERSION="1.8.3',
},
'old/path/to/sdk': {
'tools': {},
'path/to/jdk': {
'release': 'JAVA_VERSION="1.8',
},
'new/path/to/sdk': {},
'path/to/config': '{"jdkPath":"path/to/jdk","androidSdkPath":"path/to/sdk"}',
});

const mockLog = new MockLog();
const mockPrompt = new MockPrompt();
// Since 'createConfig' will be called, we push 3 future answers to 'mockPrompt'.
mockPrompt.addMessage('false'); // Should bubblewrap download the JDK?
mockPrompt.addMessage('old/path/to/jdk'); // The path of the jdk.
mockPrompt.addMessage('old/path/to/sdk'); // The path of the androidSdk.
// Create config file.
await loadOrCreateConfig(mockLog, mockPrompt);
// Change the androidSdkPath of the config file to a path which isn't a valid one.
const parsedMockArgs = minimist(['--androidSdkPath', 'new/path/to/sdk']);
await updateConfig(parsedMockArgs, mockLog);
expect(await doctor(mockLog)).toBeFalse();

await expectAsync(doctor(mockLog, 'path/to/config')).toBeResolvedTo(false);

// Check that the correct message was sent.
const logErrors: Array<string> = mockLog.getReceivedData();
expect(logErrors[logErrors.length - 1]).toEqual(messages.androidSdkPathIsNotCorrect);
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/spec/configSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('config', () => {
},
[DEFAULT_CONFIG_FOLDER]: {
'config.json':
'{"content":"some old content",' +
'{"content":"some new content",' +
' "jdkPath":"/path/to/jdk","androidSdkPath":"/path/to/android-sdk"}',
}});
const mockLog = new MockLog();
Expand All @@ -114,8 +114,8 @@ describe('config', () => {
// checks if the contents of the files didn't change.
const file1 = await fsPromises.readFile(LEGACY_CONFIG_FILE_PATH, 'utf8');
const file2 = await fsPromises.readFile(DEFAULT_CONFIG_FILE_PATH, 'utf8');
expect(file1).toEqual('{\"content\":\"some old content\"}');
expect(file2).toEqual('{\"content\":\"some new content\"}');
expect(file1.indexOf('old content')).toBeGreaterThanOrEqual(0);
expect(file2.indexOf('new content')).toBeGreaterThanOrEqual(0);
mock.restore();
});
});
Expand Down