1- import * as main from '../src/main'
2- import * as core from '@actions/core'
3- import * as exec from '@actions/exec'
4- import { ExecOptions } from '@actions/exec/lib/interfaces'
5- import * as io from '@actions/io'
6- import * as tc from '@actions/tool-cache'
1+ import type * as coreType from '@actions/core'
2+ import type * as execType from '@actions/exec'
3+ import type * as tcType from '@actions/tool-cache'
4+ import {
5+ afterEach ,
6+ beforeAll ,
7+ beforeEach ,
8+ describe ,
9+ expect ,
10+ it ,
11+ jest
12+ } from '@jest/globals'
713import * as fs from 'fs'
814import * as os from 'os'
915import * as path from 'path'
1016import * as process from 'process'
17+ import url from 'url'
18+ import type * as mainType from '../src/main'
19+ import { SpiedModule , spyOnModule } from './spy-on-module'
1120
12- const toolDir = path . join (
13- __dirname ,
14- 'runner' ,
15- path . join ( Math . random ( ) . toString ( 36 ) . substring ( 7 ) ) ,
16- 'tools'
17- )
1821const tempDir = path . join (
19- __dirname ,
22+ path . dirname ( url . fileURLToPath ( import . meta . url ) ) ,
2023 'runner' ,
21- path . join ( Math . random ( ) . toString ( 36 ) . substring ( 7 ) ) ,
22- 'temp'
24+ Math . random ( ) . toString ( 36 ) . substring ( 7 )
2325)
24-
25- process . env [ 'RUNNER_TOOL_CACHE' ] = toolDir
26- process . env [ 'RUNNER_TEMP' ] = tempDir
26+ process . env . RUNNER_TOOL_CACHE = path . join ( tempDir , 'tools' )
27+ process . env . RUNNER_TEMP = path . join ( tempDir , 'temp' )
2728
2829describe ( 'setup-edgedb' , ( ) => {
29- let inputs = { } as any
30- let inSpy : jest . SpyInstance
31- let inBooleanSpy : jest . SpyInstance
32- let cnSpy : jest . SpyInstance
33- let logSpy : jest . SpyInstance
34- let dbgSpy : jest . SpyInstance
35- let warningSpy : jest . SpyInstance
36- let dlSpy : jest . SpyInstance
37- let findSpy : jest . SpyInstance
38- let cacheSpy : jest . SpyInstance
39- let execSpy : jest . SpyInstance
40-
41- beforeEach ( ( ) => {
42- // @actions /core
30+ let inputs : Record < string , string | boolean > = { }
31+ let core : SpiedModule < typeof coreType >
32+ let exec : SpiedModule < typeof execType >
33+ let tc : SpiedModule < typeof tcType >
34+ let main : typeof mainType
35+
36+ beforeAll ( async ( ) => {
37+ core = await spyOnModule < typeof coreType > ( '@actions/core' )
38+ tc = await spyOnModule < typeof tcType > ( '@actions/tool-cache' )
39+ exec = await spyOnModule < typeof execType > ( '@actions/exec' )
40+ // After mocks have been set up
41+ main = await import ( '../src/main' )
42+ } )
43+
44+ beforeEach ( async ( ) => {
45+ // eslint-disable-next-line no-console
4346 console . log ( '::stop-commands::stoptoken' )
4447 process . env [ 'GITHUB_PATH' ] = ''
4548 inputs = {
4649 'server-dsn' : false
4750 }
48- inSpy = jest . spyOn ( core , 'getInput' )
49- inSpy . mockImplementation ( name => inputs [ name ] || '' )
50- inBooleanSpy = jest . spyOn ( core , 'getBooleanInput' )
51- inBooleanSpy . mockImplementation ( name => inputs [ name ] )
52-
53- // @actions /tool-cache
54- dlSpy = jest . spyOn ( tc , 'downloadTool' )
55- findSpy = jest . spyOn ( tc , 'find' )
56- cacheSpy = jest . spyOn ( tc , 'cacheFile' )
57-
58- // @actions /exec
59- execSpy = jest . spyOn ( exec , 'exec' )
60-
61- // writes
62- cnSpy = jest . spyOn ( process . stdout , 'write' )
63- logSpy = jest . spyOn ( core , 'info' )
64- dbgSpy = jest . spyOn ( core , 'debug' )
65- warningSpy = jest . spyOn ( core , 'warning' )
66- cnSpy . mockImplementation ( line => {
67- // uncomment to debug
68- process . stderr . write ( 'write:' + line + '\n' )
69- } )
70- logSpy . mockImplementation ( line => {
51+
52+ core . getInput . mockImplementation ( name => String ( inputs [ name ] || '' ) )
53+ core . getBooleanInput . mockImplementation ( name => Boolean ( inputs [ name ] ) )
54+
55+ core . info . mockImplementation ( line => {
7156 // uncomment to debug
72- process . stderr . write ( ' log:' + line + '\n' )
57+ process . stderr . write ( ` log:${ line } \n` )
7358 } )
74- dbgSpy . mockImplementation ( msg => {
59+ core . debug . mockImplementation ( msg => {
7560 // uncomment to see debug output
76- process . stderr . write ( msg + '\n' )
61+ process . stderr . write ( ` ${ msg } \n` )
7762 } )
7863 } )
7964
@@ -83,71 +68,72 @@ describe('setup-edgedb', () => {
8368 } )
8469
8570 it ( 'Installs CLI' , async ( ) => {
86- inputs [ 'cli-version' ] = '>=1.0.0-rc.1 <=1.0.0-rc.2 '
71+ inputs [ 'cli-version' ] = '>=3.2.0 <=3.4.0 '
8772
8873 let libc = ''
89- if ( os . platform ( ) == 'linux' ) {
74+ if ( os . platform ( ) === 'linux' ) {
9075 libc = 'musl'
9176 }
9277 const baseDist = main . getBaseDist ( os . arch ( ) , os . platform ( ) , libc )
9378 const pkgBase = `https://packages.edgedb.com/archive/${ baseDist } `
94- const expectedVer = '1.0.0-rc.2 \\+([0-9a-f]{7})'
79+ const expectedVer = '3.4.0 \\+([0-9a-f]{7})'
9580 const expectedUrl = `${ pkgBase } /edgedb-cli-${ expectedVer } `
9681
9782 const tmpdir = fs . mkdtempSync ( 'edgedb-setup' )
9883 let tmp = path . join ( tmpdir , 'foo' )
9984 fs . closeSync ( fs . openSync ( tmp , 'w' ) )
10085 tmp = fs . realpathSync ( tmp )
10186
102- dlSpy . mockImplementation ( async ( ) => tmp )
87+ tc . downloadTool . mockImplementation ( async ( ) => tmp )
10388
104- findSpy . mockImplementation ( ( ) => '' )
89+ tc . find . mockImplementation ( ( ) => '' )
10590
106- const cliPath = path . normalize ( '/cache/edgedb/1.0.0-rc.2 ' )
107- cacheSpy . mockImplementation ( async ( ) => cliPath )
91+ const cliPath = path . normalize ( '/cache/edgedb/3.4.0 ' )
92+ tc . cacheFile . mockImplementation ( async ( ) => cliPath )
10893
10994 await main . run ( )
11095
11196 fs . unlinkSync ( tmp )
11297 fs . rmdirSync ( tmpdir )
11398
114- expect ( dlSpy ) . toHaveBeenCalled ( )
115- expect ( logSpy ) . toHaveBeenCalledWith (
99+ expect ( tc . downloadTool ) . toHaveBeenCalled ( )
100+ expect ( core . info ) . toHaveBeenCalledWith (
116101 expect . stringMatching (
117102 new RegExp (
118- `Downloading edgedb-cli ${ expectedVer } - ${ os . arch } from ${ expectedUrl } `
103+ `Downloading edgedb-cli ${ expectedVer } - ${ os . arch ( ) } from ${ expectedUrl } `
119104 )
120105 )
121106 )
122- expect ( cnSpy ) . toHaveBeenCalledWith ( `::add-path:: ${ cliPath } ${ os . EOL } ` )
107+ expect ( core . addPath ) . toHaveBeenCalledWith ( cliPath )
123108 } )
124109
125110 it ( 'Installs server' , async ( ) => {
126- inputs [ 'cli-version' ] = '>=1.0.0-rc.1 <=1.0.0-rc.2 '
111+ inputs [ 'cli-version' ] = '>=3.2.0 <=3.4.0 '
127112 inputs [ 'server-version' ] = 'stable'
128113
129114 let libc = ''
130- if ( os . platform ( ) == 'linux' ) {
115+ if ( os . platform ( ) === 'linux' ) {
131116 libc = 'musl'
132117 }
133118 const baseDist = main . getBaseDist ( os . arch ( ) , os . platform ( ) , libc )
134119 const pkgBase = `https://packages.edgedb.com/archive/${ baseDist } `
135- const expectedVer = '1.0.0-rc.2 \\+([0-9a-f]{7})'
120+ const expectedVer = '3.4.0 \\+([0-9a-f]{7})'
136121 const expectedUrl = `${ pkgBase } /edgedb-cli-${ expectedVer } `
137122
138123 const tmpdir = fs . mkdtempSync ( 'edgedb-setup' )
139124 let tmp = path . join ( tmpdir , 'foo' )
140125 fs . closeSync ( fs . openSync ( tmp , 'w' ) )
141126 tmp = fs . realpathSync ( tmp )
142127
143- dlSpy . mockImplementation ( async ( ) => tmp )
128+ tc . downloadTool . mockImplementation ( async ( ) => tmp )
144129
145- findSpy . mockImplementation ( ( ) => '' )
130+ tc . find . mockImplementation ( ( ) => '' )
146131
147- execSpy . mockImplementation ( async ( cmd , args , opts : ExecOptions ) => {
148- if ( args [ 0 ] === 'server' && args [ 1 ] === 'install' ) {
132+ exec . exec . mockImplementation ( async ( cmd , args , opts ) => {
133+ if ( args && args [ 0 ] === 'server' && args [ 1 ] === 'install' ) {
149134 return 0
150135 } else if (
136+ args &&
151137 args [ 0 ] === 'server' &&
152138 args [ 1 ] === 'info' &&
153139 args [ 2 ] === '--bin-path'
@@ -161,24 +147,24 @@ describe('setup-edgedb', () => {
161147 }
162148 } )
163149
164- const cliPath = path . normalize ( '/cache/edgedb/1.0.0-rc.2 ' )
165- cacheSpy . mockImplementation ( async ( ) => cliPath )
150+ const cliPath = path . normalize ( '/cache/edgedb/3.4.0 ' )
151+ tc . cacheFile . mockImplementation ( async ( ) => cliPath )
166152 const serverPath = path . dirname ( tmp )
167153
168154 await main . run ( )
169155
170156 fs . unlinkSync ( tmp )
171157 fs . rmdirSync ( tmpdir )
172158
173- expect ( dlSpy ) . toHaveBeenCalled ( )
174- expect ( logSpy ) . toHaveBeenCalledWith (
159+ expect ( tc . downloadTool ) . toHaveBeenCalled ( )
160+ expect ( core . info ) . toHaveBeenCalledWith (
175161 expect . stringMatching (
176162 new RegExp (
177- `Downloading edgedb-cli ${ expectedVer } - ${ os . arch } from ${ expectedUrl } `
163+ `Downloading edgedb-cli ${ expectedVer } - ${ os . arch ( ) } from ${ expectedUrl } `
178164 )
179165 )
180166 )
181- expect ( cnSpy ) . toHaveBeenCalledWith ( `::add-path:: ${ serverPath } ${ os . EOL } ` )
182- expect ( cnSpy ) . toHaveBeenCalledWith ( `::add-path:: ${ cliPath } ${ os . EOL } ` )
167+ expect ( core . addPath ) . toHaveBeenCalledWith ( serverPath )
168+ expect ( core . addPath ) . toHaveBeenCalledWith ( cliPath )
183169 } )
184170} )
0 commit comments