|
| 1 | +import io = require('@actions/io'); |
| 2 | +import fs = require('fs'); |
| 3 | +import os = require('os'); |
| 4 | +import path = require('path'); |
| 5 | + |
| 6 | +const toolDir = path.join( |
| 7 | + process.cwd(), |
| 8 | + 'runner', |
| 9 | + path.join( |
| 10 | + Math.random() |
| 11 | + .toString(36) |
| 12 | + .substring(7) |
| 13 | + ), |
| 14 | + 'tools' |
| 15 | +); |
| 16 | +const tempDir = path.join( |
| 17 | + process.cwd(), |
| 18 | + 'runner', |
| 19 | + path.join( |
| 20 | + Math.random() |
| 21 | + .toString(36) |
| 22 | + .substring(7) |
| 23 | + ), |
| 24 | + 'temp' |
| 25 | +); |
| 26 | + |
| 27 | +process.env['RUNNER_TOOL_CACHE'] = toolDir; |
| 28 | +process.env['RUNNER_TEMP'] = tempDir; |
| 29 | +import * as installer from '../src/installer'; |
| 30 | + |
| 31 | +const IS_WINDOWS = process.platform === 'win32'; |
| 32 | + |
| 33 | +describe('installer tests', () => { |
| 34 | + beforeAll(async () => { |
| 35 | + await io.rmRF(toolDir); |
| 36 | + await io.rmRF(tempDir); |
| 37 | + }, 100000); |
| 38 | + |
| 39 | + afterAll(async () => { |
| 40 | + try { |
| 41 | + await io.rmRF(toolDir); |
| 42 | + await io.rmRF(tempDir); |
| 43 | + } catch { |
| 44 | + console.log('Failed to remove test directories'); |
| 45 | + } |
| 46 | + }, 100000); |
| 47 | + |
| 48 | + it('Acquires version of node if no matching version is installed', async () => { |
| 49 | + await installer.getNode('10.16.0'); |
| 50 | + const nodeDir = path.join(toolDir, 'node', '10.16.0', os.arch()); |
| 51 | + |
| 52 | + expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true); |
| 53 | + if (IS_WINDOWS) { |
| 54 | + expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true); |
| 55 | + } else { |
| 56 | + expect(fs.existsSync(path.join(nodeDir, 'bin', 'node'))).toBe(true); |
| 57 | + } |
| 58 | + }, 100000); |
| 59 | + |
| 60 | + if (IS_WINDOWS) { |
| 61 | + it('Falls back to backup location if first one doesnt contain correct version', async () => { |
| 62 | + await installer.getNode('5.10.1'); |
| 63 | + const nodeDir = path.join(toolDir, 'node', '5.10.1', os.arch()); |
| 64 | + |
| 65 | + expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true); |
| 66 | + expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true); |
| 67 | + }, 100000); |
| 68 | + |
| 69 | + it('Falls back to third location if second one doesnt contain correct version', async () => { |
| 70 | + await installer.getNode('0.12.18'); |
| 71 | + const nodeDir = path.join(toolDir, 'node', '0.12.18', os.arch()); |
| 72 | + |
| 73 | + expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true); |
| 74 | + expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true); |
| 75 | + }, 100000); |
| 76 | + } |
| 77 | + |
| 78 | + it('Throws if no location contains correct node version', async () => { |
| 79 | + let thrown = false; |
| 80 | + try { |
| 81 | + await installer.getNode('1000'); |
| 82 | + } catch { |
| 83 | + thrown = true; |
| 84 | + } |
| 85 | + expect(thrown).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('Acquires version of node with long paths', async () => { |
| 89 | + const toolpath = await installer.getNode('8.8.1'); |
| 90 | + const nodeDir = path.join(toolDir, 'node', '8.8.1', os.arch()); |
| 91 | + |
| 92 | + expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true); |
| 93 | + if (IS_WINDOWS) { |
| 94 | + expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true); |
| 95 | + } else { |
| 96 | + expect(fs.existsSync(path.join(nodeDir, 'bin', 'node'))).toBe(true); |
| 97 | + } |
| 98 | + }, 100000); |
| 99 | + |
| 100 | + it('Uses version of node installed in cache', async () => { |
| 101 | + const nodeDir: string = path.join(toolDir, 'node', '250.0.0', os.arch()); |
| 102 | + await io.mkdirP(nodeDir); |
| 103 | + fs.writeFileSync(`${nodeDir}.complete`, 'hello'); |
| 104 | + // This will throw if it doesn't find it in the cache (because no such version exists) |
| 105 | + await installer.getNode('250.0.0'); |
| 106 | + return; |
| 107 | + }); |
| 108 | + |
| 109 | + it('Doesnt use version of node that was only partially installed in cache', async () => { |
| 110 | + const nodeDir: string = path.join(toolDir, 'node', '251.0.0', os.arch()); |
| 111 | + await io.mkdirP(nodeDir); |
| 112 | + let thrown = false; |
| 113 | + try { |
| 114 | + // This will throw if it doesn't find it in the cache (because no such version exists) |
| 115 | + await installer.getNode('251.0.0'); |
| 116 | + } catch { |
| 117 | + thrown = true; |
| 118 | + } |
| 119 | + expect(thrown).toBe(true); |
| 120 | + return; |
| 121 | + }); |
| 122 | + |
| 123 | + it('Resolves semantic versions of node installed in cache', async () => { |
| 124 | + const nodeDir: string = path.join(toolDir, 'node', '252.0.0', os.arch()); |
| 125 | + await io.mkdirP(nodeDir); |
| 126 | + fs.writeFileSync(`${nodeDir}.complete`, 'hello'); |
| 127 | + // These will throw if it doesn't find it in the cache (because no such version exists) |
| 128 | + await installer.getNode('252.0.0'); |
| 129 | + await installer.getNode('252'); |
| 130 | + await installer.getNode('252.0'); |
| 131 | + }); |
| 132 | +}); |
0 commit comments