55 * functions and objects. For example, the core module is mocked in this test,
66 * so that the actual '@actions/core' module is not imported.
77 */
8+
9+ import fs from 'fs/promises'
10+ import os from 'os'
11+ import path from 'path'
12+
813import { jest } from '@jest/globals'
914import * as core from '../__fixtures__/core.js'
1015
16+ import { toolName , defaultVersion } from '../src/tool.js'
17+
1118// Mocks should be declared before the module being tested is imported.
1219jest . unstable_mockModule ( '@actions/core' , ( ) => core )
1320
@@ -16,26 +23,70 @@ jest.unstable_mockModule('@actions/core', () => core)
1623const { run } = await import ( '../src/main.js' )
1724
1825describe ( 'main.ts' , ( ) => {
19- beforeEach ( ( ) => {
26+ const OLD_ENV = process . env
27+
28+ beforeEach ( async ( ) => {
2029 // Set the action's inputs as return values from core.getInput().
21- core . getInput . mockImplementation ( ( ) => '500' )
30+ core . getInput . mockImplementation ( ( ) => defaultVersion . replaceAll ( 'v' , '' ) )
31+ process . env = { ...OLD_ENV } // Make a copy
32+
33+ switch ( os . type ( ) ) {
34+ case 'Linux' :
35+ process . env . RUNNER_OS = 'Linux'
36+ break
37+ case 'Darwin' :
38+ process . env . RUNNER_OS = 'macOS'
39+ break
40+ case 'Windows_NT' :
41+ process . env . RUNNER_OS = 'Windows'
42+ break
43+ default :
44+ throw new Error ( `Unsupported OS: ${ os . type ( ) } ` )
45+ }
46+
47+ process . env . RUNNER_ARCH = 'X86_64'
48+
49+ process . env . RUNNER_TEMP = await fs . mkdtemp (
50+ path . join ( os . tmpdir ( ) , `${ toolName } -runner-temp-` )
51+ )
52+
53+ process . env . RUNNER_TOOL_CACHE = await fs . mkdtemp (
54+ path . join ( os . tmpdir ( ) , `${ toolName } -tool-cache-` )
55+ )
2256 } )
2357
24- afterEach ( ( ) => {
58+ afterEach ( async ( ) => {
59+ if ( process . env . RUNNER_TOOL_CACHE ) {
60+ await fs . rm ( process . env . RUNNER_TOOL_CACHE , {
61+ recursive : true ,
62+ force : true
63+ } )
64+ }
65+
66+ if ( process . env . RUNNER_TEMP ) {
67+ await fs . rm ( process . env . RUNNER_TEMP , {
68+ recursive : true ,
69+ force : true
70+ } )
71+ }
72+
2573 jest . resetAllMocks ( )
74+ process . env = OLD_ENV // Restore old environment
2675 } )
2776
2877 it ( 'Run main' , async ( ) => {
2978 await run ( )
3079
31- /*
80+ // Verify that no errors were thrown.
81+ expect ( core . setFailed ) . not . toHaveBeenCalled ( )
82+
3283 // Verify the time output was set.
3384 expect ( core . setOutput ) . toHaveBeenNthCalledWith (
3485 1 ,
35- 'sops-path',
36- // Simple regex to match a time string in the format HH:MM:SS.
37- expect.stringMatching(/^sops/)
86+ 'path' ,
87+ expect . stringMatching (
88+ new RegExp ( `${ toolName } [/\\\\]${ defaultVersion . replaceAll ( 'v' , '' ) } ` )
89+ )
3890 )
39- */
4091 } )
4192} )
0 commit comments