|
| 1 | +import * as fs from 'fs' |
| 2 | +import * as path from 'path' |
| 3 | +import * as os from 'os' |
| 4 | + |
| 5 | +jest.mock( |
| 6 | + '@actions/cache', |
| 7 | + () => ({ |
| 8 | + restoreCache: jest.fn(async () => void 0), |
| 9 | + saveCache: jest.fn(async () => 0) |
| 10 | + }), |
| 11 | + {virtual: true} |
| 12 | +) |
| 13 | + |
| 14 | +jest.mock( |
| 15 | + '@actions/core', |
| 16 | + () => ({ |
| 17 | + addPath: jest.fn(() => void 0), |
| 18 | + exportVariable: jest.fn(() => void 0), |
| 19 | + info: jest.fn(() => void 0), |
| 20 | + setFailed: jest.fn(() => void 0) |
| 21 | + }), |
| 22 | + {virtual: true} |
| 23 | +) |
| 24 | + |
| 25 | +jest.mock( |
| 26 | + 'child_process', |
| 27 | + () => ({ |
| 28 | + exec: (cmd: string, cb: (err: any, stdout: any, stderr: any) => void) => { |
| 29 | + if (cmd.includes('which javac')) cb(null, '/usr/bin/javac\n', '') |
| 30 | + else if (cmd.includes('brew --prefix')) cb(null, '/opt/homebrew\n', '') |
| 31 | + else if (cmd.includes('where.exe java')) |
| 32 | + cb(null, 'C:/Program Files/Eclipse Adoptium/jdk-17/bin/java.exe\n', '') |
| 33 | + else cb(null, '', '') |
| 34 | + } |
| 35 | + }), |
| 36 | + {virtual: true} |
| 37 | +) |
| 38 | + |
| 39 | +jest.mock( |
| 40 | + '@actions/tool-cache', |
| 41 | + () => ({ |
| 42 | + find: jest.fn(() => ''), |
| 43 | + cacheDir: jest.fn(async () => '/cached') |
| 44 | + }), |
| 45 | + {virtual: true} |
| 46 | +) |
| 47 | + |
| 48 | +import { |
| 49 | + installJava, |
| 50 | + installNpmPkg, |
| 51 | + restoreCaches, |
| 52 | + saveCaches |
| 53 | +} from '../src/installer' |
| 54 | + |
| 55 | +const core = require('@actions/core') |
| 56 | +const cache = require('@actions/cache') |
| 57 | + |
| 58 | +describe('installer', () => { |
| 59 | + const originalPlatform = process.platform |
| 60 | + afterEach(() => { |
| 61 | + Object.defineProperty(process, 'platform', {value: originalPlatform}) |
| 62 | + jest.clearAllMocks() |
| 63 | + }) |
| 64 | + |
| 65 | + test('installJava sets env on linux', async () => { |
| 66 | + Object.defineProperty(process, 'platform', {value: 'linux'}) |
| 67 | + await installJava('17') |
| 68 | + expect(core.exportVariable).toHaveBeenCalledWith('JAVA_HOME', '/usr') |
| 69 | + expect(core.addPath).toHaveBeenCalledWith('/usr/bin') |
| 70 | + }) |
| 71 | + |
| 72 | + test('installNpmPkg caches bin path', async () => { |
| 73 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'installer-test-')) |
| 74 | + await installNpmPkg('@capacitor/cli', 'latest') |
| 75 | + const call = (core.addPath as any).mock.calls.find((c: any[]) => { |
| 76 | + const arg = String(c[0]) |
| 77 | + return arg.includes('node_modules') && arg.includes('.bin') |
| 78 | + }) |
| 79 | + expect(call).toBeTruthy() |
| 80 | + fs.rmdirSync(tmp, {recursive: true}) |
| 81 | + }) |
| 82 | + |
| 83 | + test('android sdk cache restore/save with env root', async () => { |
| 84 | + const sdkRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'android-sdk-')) |
| 85 | + fs.mkdirSync(path.join(sdkRoot, 'platforms')) |
| 86 | + fs.mkdirSync(path.join(sdkRoot, 'build-tools')) |
| 87 | + process.env.ANDROID_SDK_ROOT = sdkRoot |
| 88 | + await restoreCaches() |
| 89 | + await saveCaches() |
| 90 | + const restoreCall = (cache.restoreCache as any).mock.calls.find( |
| 91 | + (c: any[]) => Array.isArray(c[0]) && c[0][0] === sdkRoot |
| 92 | + ) |
| 93 | + const saveCall = (cache.saveCache as any).mock.calls.find( |
| 94 | + (c: any[]) => Array.isArray(c[0]) && c[0][0] === sdkRoot |
| 95 | + ) |
| 96 | + expect(restoreCall).toBeTruthy() |
| 97 | + expect(saveCall).toBeTruthy() |
| 98 | + fs.rmdirSync(sdkRoot, {recursive: true}) |
| 99 | + delete process.env.ANDROID_SDK_ROOT |
| 100 | + }) |
| 101 | +}) |
0 commit comments