11import axios from "axios" ;
22import axiosRetry from "axios-retry" ;
3- import extractZip from "extract-zip" ;
43import { mocked } from "jest-mock" ;
54import fs , { WriteStream } from "node:fs" ;
65import os from "node:os" ;
@@ -10,6 +9,7 @@ import { fakeModuleFile } from "../test/mock-template-files";
109import { expectThrownError } from "../test/util" ;
1110import {
1211 ArchiveDownloadError ,
12+ MissingModuleFileError ,
1313 ReleaseArchive ,
1414 UnsupportedArchiveFormat ,
1515} from "./release-archive" ;
@@ -24,6 +24,7 @@ jest.mock("extract-zip");
2424const RELEASE_ARCHIVE_URL = "https://foo.bar/rules-foo-v1.2.3.tar.gz" ;
2525const STRIP_PREFIX = "rules-foo" ;
2626const TEMP_DIR = "/tmp" ;
27+ const EXTRACT_DIR = `${ TEMP_DIR } /archive-1234` ;
2728
2829beforeEach ( ( ) => {
2930 jest . clearAllMocks ( ) ;
@@ -50,6 +51,9 @@ beforeEach(() => {
5051 ) ;
5152
5253 mocked ( os . tmpdir ) . mockReturnValue ( TEMP_DIR ) ;
54+ mocked ( fs . mkdtempSync ) . mockReturnValue ( EXTRACT_DIR ) ;
55+
56+ mocked ( fs . existsSync ) . mockReturnValue ( true ) ; // Existence check on MODULE.bazel
5357} ) ;
5458
5559describe ( "fetch" , ( ) => {
@@ -73,7 +77,7 @@ describe("fetch", () => {
7377 test ( "saves the archive to disk" , async ( ) => {
7478 await ReleaseArchive . fetch ( RELEASE_ARCHIVE_URL , STRIP_PREFIX ) ;
7579
76- const expectedPath = path . join ( TEMP_DIR , "rules-foo-v1.2.3.tar.gz" ) ;
80+ const expectedPath = path . join ( EXTRACT_DIR , "rules-foo-v1.2.3.tar.gz" ) ;
7781 expect ( fs . createWriteStream ) . toHaveBeenCalledWith ( expectedPath , {
7882 flags : "w" ,
7983 } ) ;
@@ -94,7 +98,7 @@ describe("fetch", () => {
9498 STRIP_PREFIX
9599 ) ;
96100
97- const expectedPath = path . join ( TEMP_DIR , "rules-foo-v1.2.3.tar.gz" ) ;
101+ const expectedPath = path . join ( EXTRACT_DIR , "rules-foo-v1.2.3.tar.gz" ) ;
98102 expect ( releaseArchive . diskPath ) . toEqual ( expectedPath ) ;
99103 } ) ;
100104
@@ -161,42 +165,7 @@ describe("extractModuleFile", () => {
161165 expect ( tar . x ) . toHaveBeenCalledWith ( {
162166 cwd : path . dirname ( releaseArchive . diskPath ) ,
163167 file : releaseArchive . diskPath ,
164- strip : 1 ,
165- } ) ;
166- } ) ;
167-
168- test ( "extracts a tarball when the strip_prefix is empty" , async ( ) => {
169- const releaseArchive = await ReleaseArchive . fetch (
170- "https://foo.bar/rules-foo-v1.2.3.tar.gz" ,
171- ""
172- ) ;
173- await releaseArchive . extractModuleFile ( "." ) ;
174-
175- expect ( tar . x ) . toHaveBeenCalledWith ( {
176- cwd : path . dirname ( releaseArchive . diskPath ) ,
177- file : releaseArchive . diskPath ,
178- strip : 0 ,
179- } ) ;
180- } ) ;
181-
182- test ( "extracts the full zip archive next to the zip archive" , async ( ) => {
183- const releaseArchive = await ReleaseArchive . fetch (
184- "https://foo.bar/rules-foo-v1.2.3.zip" ,
185- STRIP_PREFIX
186- ) ;
187- await releaseArchive . extractModuleFile ( "." ) ;
188-
189- expect ( extractZip ) . toHaveBeenCalledWith ( releaseArchive . diskPath , {
190- dir : path . dirname ( releaseArchive . diskPath ) ,
191168 } ) ;
192- expect ( fs . copyFileSync ) . toHaveBeenCalledWith (
193- path . join (
194- path . dirname ( releaseArchive . diskPath ) ,
195- STRIP_PREFIX ,
196- "MODULE.bazel"
197- ) ,
198- path . join ( path . dirname ( releaseArchive . diskPath ) , "MODULE.bazel" )
199- ) ;
200169 } ) ;
201170
202171 test ( "loads the extracted MODULE.bazel file" , async ( ) => {
@@ -208,6 +177,7 @@ describe("extractModuleFile", () => {
208177
209178 const expectedPath = path . join (
210179 path . dirname ( releaseArchive . diskPath ) ,
180+ STRIP_PREFIX ,
211181 "MODULE.bazel"
212182 ) ;
213183 expect ( fs . readFileSync ) . toHaveBeenCalledWith ( expectedPath , "utf8" ) ;
@@ -222,6 +192,7 @@ describe("extractModuleFile", () => {
222192
223193 const expectedPath = path . join (
224194 path . dirname ( releaseArchive . diskPath ) ,
195+ STRIP_PREFIX ,
225196 "sub" ,
226197 "dir" ,
227198 "MODULE.bazel"
@@ -239,4 +210,17 @@ describe("extractModuleFile", () => {
239210 expect ( moduleFile . moduleName ) . toEqual ( "rules_foo" ) ;
240211 expect ( moduleFile . version ) . toEqual ( "1.2.3" ) ;
241212 } ) ;
213+
214+ test ( "throws when MODULE.bazel cannot be found in the release archive" , async ( ) => {
215+ const releaseArchive = await ReleaseArchive . fetch (
216+ RELEASE_ARCHIVE_URL ,
217+ STRIP_PREFIX
218+ ) ;
219+
220+ mocked ( fs . existsSync ) . mockReturnValue ( false ) ;
221+
222+ await expect ( releaseArchive . extractModuleFile ( "." ) ) . rejects . toThrow (
223+ MissingModuleFileError
224+ ) ;
225+ } ) ;
242226} ) ;
0 commit comments