import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, test, vi } from 'vitest'; import Versioning from './versioning'; import UnityVersioning from './unity-versioning'; import AndroidVersioning from './android-versioning'; import BuildParameters from './build-parameters'; import Input from './input'; import Platform from './platform'; const testLicense = '\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \nm0Db8UK+ktnOLJBtHybkfetpcKo=o/pUbSQAukz7+ZYAWhnA0AJbIlyyCPL7bKVEM2lVqbrXt7cyey+umkCXamuOgsWPVUKBMkXtMH8L\n5etLmD0getWIhTGhzOnDCk+gtIPfL4jMo9tkEuOCROQAXCci23VFscKcrkB+3X6h4wEOtA2APhOY\nB+wvC794o8/82ffjP79aVAi57rp3Wmzx+9pe9yMwoJuljAy2sc2tIMgdQGWVmOGBpQm3JqsidyzI\nJWG2kjnc7pDXK9pwYzXoKiqUqqrut90d+kQqRyv7MSZXR50HFqD/LI69h68b7P8Bjo3bPXOhNXGR\n9YCoemH6EkfCJxp2gIjzjWW+l2Hj2EsFQi8YXw=='; afterEach(() => { vi.clearAllMocks(); vi.restoreAllMocks(); }); beforeEach(() => { vi.spyOn(Versioning, 'determineBuildVersion').mockImplementation(async () => '1.3.37'); process.env.UNITY_LICENSE = testLicense; // Todo - Don't use process.env directly, that's what the input model class is for. }); describe('BuildParameters', () => { describe('create', () => { it('does not throw', async () => { await expect(BuildParameters.create()).resolves.not.toThrow(); }); it('determines the version only once', async () => { vi.spyOn(Versioning, 'determineBuildVersion').mockImplementation(async () => '1.3.37'); await BuildParameters.create(); await expect(Versioning.determineBuildVersion).toHaveBeenCalledTimes(1); }); it('determines the unity version only once', async () => { vi.spyOn(UnityVersioning, 'determineUnityVersion').mockImplementation(() => '2019.2.11f1'); await BuildParameters.create(); expect(UnityVersioning.determineUnityVersion).toHaveBeenCalledTimes(1); }); it('returns the android version code with provided input', async () => { const mockValue = '42'; vi.spyOn(Input, 'androidVersionCode', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ androidVersionCode: mockValue }), ); }); it('returns the android version code from version by default', async () => { const mockValue = ''; vi.spyOn(Input, 'androidVersionCode', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ androidVersionCode: '1003037' }), ); }); it('determines the android sdk manager parameters only once', async () => { vi.spyOn(AndroidVersioning, 'determineSdkManagerParameters').mockImplementation( () => 'platforms;android-30', ); await BuildParameters.create(); expect(AndroidVersioning.determineSdkManagerParameters).toHaveBeenCalledTimes(1); }); it('returns the targetPlatform', async () => { const mockValue = 'somePlatform'; vi.spyOn(Input, 'targetPlatform', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ targetPlatform: mockValue }), ); }); it('returns the project path', async () => { const mockValue = 'path/to/project'; vi.spyOn(UnityVersioning, 'determineUnityVersion').mockImplementation(() => '2019.2.11f1'); vi.spyOn(Input, 'projectPath', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ projectPath: mockValue }), ); }); it('returns the build profile', async () => { const mockValue = 'path/to/build_profile.asset'; vi.spyOn(Input, 'buildProfile', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ buildProfile: mockValue }), ); }); it('returns the build name', async () => { const mockValue = 'someBuildName'; vi.spyOn(Input, 'buildName', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ buildName: mockValue }), ); }); it('returns the build path', async () => { const mockPath = 'somePath'; const mockPlatform = 'somePlatform'; const expectedBuildPath = `${mockPath}/${mockPlatform}`; vi.spyOn(Input, 'buildsPath', 'get').mockReturnValue(mockPath); vi.spyOn(Input, 'targetPlatform', 'get').mockReturnValue(mockPlatform); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ buildPath: expectedBuildPath }), ); }); it('returns the build file', async () => { const mockValue = 'someBuildName'; const mockPlatform = 'somePlatform'; vi.spyOn(Input, 'buildName', 'get').mockReturnValue(mockValue); vi.spyOn(Input, 'targetPlatform', 'get').mockReturnValue(mockPlatform); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ buildFile: mockValue }), ); }); test.each` targetPlatform | expectedExtension | androidExportType | linux64RemoveExecutableExtension ${Platform.types.Android} | ${'.apk'} | ${'androidPackage'} | ${false} ${Platform.types.Android} | ${'.aab'} | ${'androidAppBundle'} | ${true} ${Platform.types.Android} | ${''} | ${'androidStudioProject'} | ${false} ${Platform.types.StandaloneWindows} | ${'.exe'} | ${'n/a'} | ${true} ${Platform.types.StandaloneWindows64} | ${'.exe'} | ${'n/a'} | ${false} ${Platform.types.StandaloneLinux64} | ${'.x86_64'} | ${'n/a'} | ${false} ${Platform.types.StandaloneLinux64} | ${''} | ${'n/a'} | ${true} `( 'appends $expectedExtension for $targetPlatform with linux64RemoveExecutableExtension=$linux64RemoveExecutableExtension', async ({ targetPlatform, expectedExtension, androidExportType, linux64RemoveExecutableExtension }) => { vi.spyOn(Input, 'targetPlatform', 'get').mockReturnValue(targetPlatform); vi.spyOn(Input, 'buildName', 'get').mockReturnValue(targetPlatform); vi.spyOn(Input, 'androidExportType', 'get').mockReturnValue(androidExportType); vi.spyOn(Input, 'linux64RemoveExecutableExtension', 'get').mockReturnValue(linux64RemoveExecutableExtension); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ buildFile: `${targetPlatform}${expectedExtension}` }), ); }, ); test.each` targetPlatform | androidSymbolType ${Platform.types.Android} | ${'none'} ${Platform.types.Android} | ${'public'} ${Platform.types.Android} | ${'debugging'} ${Platform.types.StandaloneWindows} | ${'none'} ${Platform.types.StandaloneWindows64} | ${'none'} `( 'androidSymbolType is set to $androidSymbolType when targetPlatform is $targetPlatform and input targetSymbolType is $androidSymbolType', async ({ targetPlatform, androidSymbolType }) => { vi.spyOn(Input, 'targetPlatform', 'get').mockReturnValue(targetPlatform); vi.spyOn(Input, 'androidSymbolType', 'get').mockReturnValue(androidSymbolType); vi.spyOn(Input, 'buildName', 'get').mockReturnValue(targetPlatform); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ androidSymbolType }), ); }, ); it('returns the build method', async () => { const mockValue = 'Namespace.ClassName.BuildMethod'; vi.spyOn(Input, 'buildMethod', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ buildMethod: mockValue }), ); }); it('returns the android keystore name', async () => { const mockValue = 'keystore.keystore'; vi.spyOn(Input, 'androidKeystoreName', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ androidKeystoreName: mockValue }), ); }); it('returns the android keystore base64-encoded content', async () => { const mockValue = 'secret'; vi.spyOn(Input, 'androidKeystoreBase64', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ androidKeystoreBase64: mockValue }), ); }); it('returns the android keystore pass', async () => { const mockValue = 'secret'; vi.spyOn(Input, 'androidKeystorePass', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ androidKeystorePass: mockValue }), ); }); it('returns the android keyalias name', async () => { const mockValue = 'secret'; vi.spyOn(Input, 'androidKeyaliasName', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ androidKeyaliasName: mockValue }), ); }); it('returns the android keyalias pass', async () => { const mockValue = 'secret'; vi.spyOn(Input, 'androidKeyaliasPass', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ androidKeyaliasPass: mockValue }), ); }); it('returns the android target sdk version', async () => { const mockValue = 'AndroidApiLevelAuto'; vi.spyOn(Input, 'androidTargetSdkVersion', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ androidTargetSdkVersion: mockValue }), ); }); it('returns the unity licensing server address', async () => { const mockValue = 'http://example.com'; vi.spyOn(Input, 'unityLicensingServer', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ unityLicensingServer: mockValue }), ); }); it('throws error when no unity license provider provided', async () => { delete process.env.UNITY_LICENSE; // Need to delete this as it is set for every test currently await expect(BuildParameters.create()).rejects.toThrowError(); }); it('return serial when no license server is provided', async () => { const mockValue = '123'; delete process.env.UNITY_LICENSE; // Need to delete this as it is set for every test currently process.env.UNITY_SERIAL = mockValue; await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ unitySerial: mockValue }), ); delete process.env.UNITY_SERIAL; }); it('returns the custom parameters', async () => { const mockValue = '-profile SomeProfile -someBoolean -someValue exampleValue'; vi.spyOn(Input, 'customParameters', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ customParameters: mockValue }), ); }); it.each([true, false])('returns the flag for useHostNetwork when %s', async (mockValue) => { vi.spyOn(Input, 'useHostNetwork', 'get').mockReturnValue(mockValue); await expect(BuildParameters.create()).resolves.toEqual( expect.objectContaining({ useHostNetwork: mockValue }), ); }); }); });