forked from actions/setup-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-ruby.test.ts
74 lines (62 loc) · 2.33 KB
/
find-ruby.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import * as tc from '@actions/tool-cache';
import * as core from '@actions/core';
import fs = require('fs');
import os = require('os');
import path = require('path');
import * as cache from '../src/cache';
import {run} from '../src/main';
describe('find-ruby', () => {
let inSpy: jest.SpyInstance;
let tcSpy: jest.SpyInstance;
let cnSpy: jest.SpyInstance;
beforeAll(async () => {
process.env['GITHUB_PATH'] = ''; // Stub out ENV file functionality so we can verify it writes to standard out
console.log('::stop-commands::stoptoken'); // Disable executing of runner commands when running tests in actions
});
beforeEach(() => {
tcSpy = jest.spyOn(tc, 'find');
inSpy = jest.spyOn(core, 'getInput');
cnSpy = jest.spyOn(process.stdout, 'write');
cnSpy.mockImplementation(line => {
// uncomment to debug
// process.stderr.write('write2:' + line + '\n');
});
});
afterEach(() => {
tcSpy.mockClear();
cnSpy.mockClear();
jest.clearAllMocks();
});
afterAll(async () => {
console.log('::stoptoken::'); // Re-enable executing of runner commands when running tests in actions
}, 100000);
it('finds a version of ruby already in the cache', async () => {
let toolPath = path.normalize('/cache/ruby/2.7.0/x64');
tcSpy.mockImplementation(() => toolPath);
let cacheDir: string = await cache.find('2.7.0');
let expPath = path.join(toolPath, 'bin');
expect(cacheDir).toBe(expPath);
});
it('does not find a version of ruby not in the cache', async () => {
tcSpy.mockImplementation(() => '');
let cacheDir: string = await cache.find('2.9.9');
expect(cacheDir).toBeFalsy();
});
it('finds a version in the cache and adds it to the path', async () => {
let toolPath = path.normalize('/cache/ruby/2.7.0/x64');
inSpy.mockImplementation(() => '2.7.0');
tcSpy.mockImplementation(() => toolPath);
await run();
let expPath = path.join(toolPath, 'bin');
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${os.EOL}`);
});
it('handles unhandled error and reports error', async () => {
let errMsg = 'unhandled error message';
inSpy.mockImplementation(() => '2.7.0');
tcSpy.mockImplementation(() => {
throw new Error(errMsg);
});
await run();
expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + os.EOL);
});
});