@@ -10,10 +10,18 @@ import {VERSION} from '../src/version'
1010const toolPath = join ( __dirname , 'runner' , 'tools' , 'leiningen' )
1111const tempPath = join ( __dirname , 'runner' , 'temp' , 'leiningen' )
1212const downloadPath = join ( __dirname , 'runner' , 'download' )
13+ const jarDownloadPath = join ( __dirname , 'runner' , 'download' , 'leiningen.jar' )
14+ const zipDownloadPath = join ( __dirname , 'runner' , 'download' , 'leiningen.zip' )
1315const cachePath = join ( __dirname , 'runner' , 'cache' )
1416
1517import * as leiningen from '../src/leiningen'
1618
19+ function httpError ( statusCode : number ) : Error & { httpStatusCode : number } {
20+ return Object . assign ( new Error ( `Unexpected HTTP response: ${ statusCode } ` ) , {
21+ httpStatusCode : statusCode
22+ } )
23+ }
24+
1725jest . mock ( '@actions/core' )
1826const core : jest . Mocked < typeof _core > = _core as never
1927
@@ -44,6 +52,7 @@ describe('leiningen tests', () => {
4452 afterEach ( async ( ) => {
4553 jest . spyOn ( global . Math , 'random' ) . mockRestore ( )
4654 jest . resetAllMocks ( )
55+ global . fetch = undefined as never
4756 delete process . env [ 'RUNNER_TOOL_CACHE' ]
4857 delete process . env [ 'RUNNER_TEMP' ]
4958 } )
@@ -55,12 +64,21 @@ describe('leiningen tests', () => {
5564 } )
5665
5766 it ( 'Install leiningen with normal version' , async ( ) => {
67+ // First call downloads lein script, second downloads the JAR
5868 tc . downloadTool . mockResolvedValueOnce ( downloadPath )
69+ tc . downloadTool . mockResolvedValueOnce ( jarDownloadPath )
5970 fs . stat . mockResolvedValueOnce ( { isFile : ( ) => true } as never )
6071 tc . cacheDir . mockResolvedValueOnce ( cachePath )
6172
6273 await leiningen . setup ( '2.9.1' )
6374
75+ // Verify JAR was downloaded from GitHub releases
76+ expect ( tc . downloadTool ) . toHaveBeenCalledWith (
77+ 'https://github.com/technomancy/leiningen/releases/download/2.9.1/leiningen-2.9.1-standalone.jar' ,
78+ expect . any ( String ) ,
79+ undefined
80+ )
81+
6482 expect ( io . mkdirP ) . toHaveBeenNthCalledWith (
6583 1 ,
6684 join ( tempPath , 'temp_2000000000' )
@@ -69,6 +87,22 @@ describe('leiningen tests', () => {
6987 2 ,
7088 join ( tempPath , 'temp_2000000000' , 'leiningen' , 'bin' )
7189 )
90+ // Verify self-installs directory was created
91+ expect ( io . mkdirP ) . toHaveBeenNthCalledWith (
92+ 3 ,
93+ join ( tempPath , 'temp_2000000000' , 'leiningen' , 'self-installs' )
94+ )
95+ // Verify JAR was moved to self-installs
96+ expect ( io . mv ) . toHaveBeenCalledWith (
97+ jarDownloadPath ,
98+ join (
99+ tempPath ,
100+ 'temp_2000000000' ,
101+ 'leiningen' ,
102+ 'self-installs' ,
103+ 'leiningen-2.9.1-standalone.jar'
104+ )
105+ )
72106 expect ( exec . exec . mock . calls [ 0 ] ) . toMatchObject ( [
73107 './lein version' ,
74108 [ ] ,
@@ -92,12 +126,33 @@ describe('leiningen tests', () => {
92126 } )
93127
94128 it ( 'Install latest leiningen' , async ( ) => {
129+ // Mock fetch for getting latest version
130+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
131+ ok : true ,
132+ json : jest . fn ( ) . mockResolvedValue ( { tag_name : '2.12.0' } )
133+ } )
134+
135+ // First call downloads lein script, second downloads the JAR
95136 tc . downloadTool . mockResolvedValueOnce ( downloadPath )
137+ tc . downloadTool . mockResolvedValueOnce ( jarDownloadPath )
96138 fs . stat . mockResolvedValueOnce ( { isFile : ( ) => true } as never )
97139 tc . cacheDir . mockResolvedValueOnce ( cachePath )
98140
99141 await leiningen . setup ( 'latest' )
100142
143+ // Verify latest version was fetched
144+ expect ( global . fetch ) . toHaveBeenCalledWith (
145+ 'https://api.github.com/repos/technomancy/leiningen/releases/latest' ,
146+ expect . any ( Object )
147+ )
148+
149+ // Verify JAR was downloaded with resolved version
150+ expect ( tc . downloadTool ) . toHaveBeenCalledWith (
151+ 'https://github.com/technomancy/leiningen/releases/download/2.12.0/leiningen-2.12.0-standalone.jar' ,
152+ expect . any ( String ) ,
153+ undefined
154+ )
155+
101156 expect ( io . mkdirP ) . toHaveBeenNthCalledWith (
102157 1 ,
103158 join ( tempPath , 'temp_2000000000' )
@@ -128,6 +183,66 @@ describe('leiningen tests', () => {
128183 expect ( core . addPath ) . toHaveBeenCalledWith ( join ( cachePath , 'bin' ) )
129184 } )
130185
186+ it ( 'Falls back to zip artifact when jar returns 404' , async ( ) => {
187+ tc . downloadTool . mockResolvedValueOnce ( downloadPath )
188+ tc . downloadTool . mockRejectedValueOnce ( httpError ( 404 ) )
189+ tc . downloadTool . mockResolvedValueOnce ( zipDownloadPath )
190+ fs . stat . mockResolvedValueOnce ( { isFile : ( ) => true } as never )
191+ tc . cacheDir . mockResolvedValueOnce ( cachePath )
192+
193+ await leiningen . setup ( '2.9.1' )
194+
195+ expect ( tc . downloadTool ) . toHaveBeenNthCalledWith (
196+ 2 ,
197+ 'https://github.com/technomancy/leiningen/releases/download/2.9.1/leiningen-2.9.1-standalone.jar' ,
198+ join ( tempPath , 'leiningen-2.9.1-standalone.jar' ) ,
199+ undefined
200+ )
201+ expect ( tc . downloadTool ) . toHaveBeenNthCalledWith (
202+ 3 ,
203+ 'https://github.com/technomancy/leiningen/releases/download/2.9.1/leiningen-2.9.1-standalone.zip' ,
204+ join ( tempPath , 'leiningen-2.9.1-standalone.zip' ) ,
205+ undefined
206+ )
207+ expect ( io . mv ) . toHaveBeenCalledWith (
208+ zipDownloadPath ,
209+ join ( tempPath , 'leiningen-2.9.1-standalone.jar' )
210+ )
211+ expect ( io . mv ) . toHaveBeenCalledWith (
212+ join ( tempPath , 'leiningen-2.9.1-standalone.jar' ) ,
213+ join (
214+ tempPath ,
215+ 'temp_2000000000' ,
216+ 'leiningen' ,
217+ 'self-installs' ,
218+ 'leiningen-2.9.1-standalone.jar'
219+ )
220+ )
221+ } )
222+
223+ it ( 'Fails when both jar and zip artifacts return 404' , async ( ) => {
224+ tc . downloadTool . mockResolvedValueOnce ( downloadPath )
225+ tc . downloadTool . mockRejectedValueOnce ( httpError ( 404 ) )
226+ tc . downloadTool . mockRejectedValueOnce ( httpError ( 404 ) )
227+
228+ await expect ( leiningen . setup ( '2.9.1' ) ) . rejects . toThrow (
229+ 'Unexpected HTTP response: 404'
230+ )
231+
232+ expect ( tc . downloadTool ) . toHaveBeenNthCalledWith (
233+ 2 ,
234+ 'https://github.com/technomancy/leiningen/releases/download/2.9.1/leiningen-2.9.1-standalone.jar' ,
235+ join ( tempPath , 'leiningen-2.9.1-standalone.jar' ) ,
236+ undefined
237+ )
238+ expect ( tc . downloadTool ) . toHaveBeenNthCalledWith (
239+ 3 ,
240+ 'https://github.com/technomancy/leiningen/releases/download/2.9.1/leiningen-2.9.1-standalone.zip' ,
241+ join ( tempPath , 'leiningen-2.9.1-standalone.zip' ) ,
242+ undefined
243+ )
244+ } )
245+
131246 it ( 'Uses version of leiningen installed in cache' , async ( ) => {
132247 tc . find . mockReturnValue ( cachePath )
133248 await leiningen . setup ( '2.9.1' )
0 commit comments