|
| 1 | +import { Yok } from "../lib/common/yok"; |
| 2 | +import { AndroidToolsInfo } from "../lib/android-tools-info"; |
| 3 | +import { EOL } from "os"; |
| 4 | +import { format } from "util"; |
| 5 | +import { assert } from "chai"; |
| 6 | + |
| 7 | +interface ITestData { |
| 8 | + javacVersion: string; |
| 9 | + expectedResult: boolean; |
| 10 | + warnings?: string[]; |
| 11 | +} |
| 12 | + |
| 13 | +describe("androidToolsInfo", () => { |
| 14 | + let loggedWarnings: string[] = []; |
| 15 | + let loggedMarkdownMessages: string[] = []; |
| 16 | + const sysRequirementsLink = ""; |
| 17 | + |
| 18 | + const additionalMsg = "You will not be able to build your projects for Android." + EOL |
| 19 | + + "To be able to build for Android, verify that you have installed The Java Development Kit (JDK) and configured it according to system requirements as" + EOL + |
| 20 | + " described in " + sysRequirementsLink; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + loggedWarnings = []; |
| 24 | + loggedMarkdownMessages = []; |
| 25 | + }); |
| 26 | + |
| 27 | + const createTestInjector = (): IInjector => { |
| 28 | + const testInjector = new Yok(); |
| 29 | + testInjector.register("childProcess", {}); |
| 30 | + testInjector.register("errors", { |
| 31 | + failWithoutHelp: (message: string, ...args: any[]): any => { |
| 32 | + const loggedError = format(message, args); |
| 33 | + throw new Error(loggedError); |
| 34 | + } |
| 35 | + }); |
| 36 | + testInjector.register("fs", {}); |
| 37 | + testInjector.register("hostInfo", {}); |
| 38 | + testInjector.register("logger", { |
| 39 | + warn: (...args: string[]): void => { |
| 40 | + loggedWarnings.push(format.apply(null, args)); |
| 41 | + }, |
| 42 | + |
| 43 | + printMarkdown: (...args: string[]): void => { |
| 44 | + loggedMarkdownMessages.push(format.apply(null, args)); |
| 45 | + } |
| 46 | + }); |
| 47 | + testInjector.register("options", {}); |
| 48 | + testInjector.register("staticConfig", { |
| 49 | + SYS_REQUIREMENTS_LINK: sysRequirementsLink |
| 50 | + }); |
| 51 | + return testInjector; |
| 52 | + }; |
| 53 | + |
| 54 | + describe("validateJavacVersion", () => { |
| 55 | + const testData: ITestData[] = [ |
| 56 | + { |
| 57 | + javacVersion: "1.8.0", |
| 58 | + expectedResult: false |
| 59 | + }, |
| 60 | + { |
| 61 | + javacVersion: "1.8.0_152", |
| 62 | + expectedResult: false |
| 63 | + }, |
| 64 | + { |
| 65 | + javacVersion: "9", |
| 66 | + expectedResult: true, |
| 67 | + warnings: ["Javac version 9 is not supported. You have to install version 1.8.0."] |
| 68 | + }, |
| 69 | + { |
| 70 | + javacVersion: "9.0.1", |
| 71 | + expectedResult: true, |
| 72 | + warnings: ["Javac version 9.0.1 is not supported. You have to install version 1.8.0."] |
| 73 | + }, |
| 74 | + { |
| 75 | + javacVersion: "1.7.0", |
| 76 | + expectedResult: true, |
| 77 | + warnings: ["Javac version 1.7.0 is not supported. You have to install at least 1.8.0."] |
| 78 | + }, |
| 79 | + { |
| 80 | + javacVersion: "1.7.0_132", |
| 81 | + expectedResult: true, |
| 82 | + warnings: ["Javac version 1.7.0_132 is not supported. You have to install at least 1.8.0."] |
| 83 | + }, |
| 84 | + { |
| 85 | + javacVersion: null, |
| 86 | + expectedResult: true, |
| 87 | + warnings: ["Error executing command 'javac'. Make sure you have installed The Java Development Kit (JDK) and set JAVA_HOME environment variable."] |
| 88 | + } |
| 89 | + ]; |
| 90 | + |
| 91 | + _.each(testData, ({ javacVersion, expectedResult, warnings }) => { |
| 92 | + it(`returns ${expectedResult} when version is ${javacVersion}`, () => { |
| 93 | + const testInjector = createTestInjector(); |
| 94 | + const androidToolsInfo = testInjector.resolve<IAndroidToolsInfo>(AndroidToolsInfo); |
| 95 | + assert.deepEqual(androidToolsInfo.validateJavacVersion(javacVersion), expectedResult); |
| 96 | + if (warnings && warnings.length) { |
| 97 | + assert.deepEqual(loggedWarnings, warnings); |
| 98 | + assert.deepEqual(loggedMarkdownMessages, [additionalMsg]); |
| 99 | + } else { |
| 100 | + assert.equal(loggedWarnings.length, 0); |
| 101 | + assert.equal(loggedMarkdownMessages.length, 0); |
| 102 | + } |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("throws error when passing showWarningsAsErrors to true and javac is not installed", () => { |
| 107 | + const testInjector = createTestInjector(); |
| 108 | + const androidToolsInfo = testInjector.resolve<IAndroidToolsInfo>(AndroidToolsInfo); |
| 109 | + assert.throws(() => androidToolsInfo.validateJavacVersion(null, { showWarningsAsErrors: true }), "Error executing command 'javac'. Make sure you have installed The Java Development Kit (JDK) and set JAVA_HOME environment variable."); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments