|
10 | 10 | */ |
11 | 11 |
|
12 | 12 | import crypto from "node:crypto"; |
| 13 | +import fsp from "node:fs/promises"; |
13 | 14 | import { tmpdir } from "node:os"; |
14 | 15 | import path from "node:path"; |
| 16 | +import archive from "@src/archive"; |
| 17 | +import utils from "@src/utils"; |
15 | 18 | import type * as t from "@types"; |
| 19 | +import { type MockInstance, vi } from "vitest"; |
16 | 20 |
|
17 | 21 | export function getTempDirPath() { |
18 | | - const randString = crypto.randomBytes(8).toString("hex"); |
| 22 | + const randString = crypto.randomBytes(16).toString("hex"); |
19 | 23 | return path.join(tmpdir(), "vitest", randString); |
20 | 24 | } |
21 | 25 |
|
@@ -51,3 +55,54 @@ export function getMustDownload(prettyName1?: string, prettyName2?: string): t.P |
51 | 55 | } as t.ProcessorReturn, |
52 | 56 | ]; |
53 | 57 | } |
| 58 | + |
| 59 | +export interface SetupArgs { |
| 60 | + newFileExists?: boolean; |
| 61 | + oldFileExists?: boolean; |
| 62 | + alwaysPull?: boolean; |
| 63 | + decompress?: boolean | t.DecompressionOptions; |
| 64 | + decompFilesExist?: boolean; |
| 65 | + filesList?: string[]; |
| 66 | +} |
| 67 | + |
| 68 | +export type SetupReturn = [t.RemoteBlobOption, t.HistoryFileContents, MockInstance]; |
| 69 | + |
| 70 | +export function setupProcessorTest(args?: SetupArgs): SetupReturn { |
| 71 | + const option: t.RemoteBlobOption = { |
| 72 | + url: "https://example.com/file.zip", |
| 73 | + dest: "some/path/testFile.zip", |
| 74 | + alwaysPull: args?.alwaysPull ?? false, |
| 75 | + decompress: args?.decompress ?? false, |
| 76 | + }; |
| 77 | + const blobDigest = utils.digestRemoteBlobOption(option); |
| 78 | + const decompDigest = archive.digestDecompressionOptions(option.decompress); |
| 79 | + const contents: t.HistoryFileContents = { |
| 80 | + [blobDigest]: { |
| 81 | + url: "https://example.com/file.zip", |
| 82 | + dest: "/some/path/testFile.zip", |
| 83 | + blobOptionsDigest: blobDigest, |
| 84 | + decompression: { |
| 85 | + optionsDigest: decompDigest, |
| 86 | + filesList: args?.filesList ?? [], |
| 87 | + }, |
| 88 | + }, |
| 89 | + }; |
| 90 | + const getDestDetails = (exists?: boolean) => { |
| 91 | + return { |
| 92 | + filePath: "/some/path/testFile.zip", |
| 93 | + fileExists: !!exists, |
| 94 | + dirPath: "/some/path", |
| 95 | + dirExists: !!exists, |
| 96 | + fileName: "testFile.zip", |
| 97 | + }; |
| 98 | + }; |
| 99 | + vi.spyOn(utils, "getDestDetails") |
| 100 | + .mockImplementationOnce(() => getDestDetails(args?.newFileExists)) |
| 101 | + .mockImplementationOnce(() => getDestDetails(args?.oldFileExists)); |
| 102 | + if (args?.decompress) { |
| 103 | + vi.spyOn(archive, "allDecompressedFilesExist").mockResolvedValue(!!args?.decompFilesExist); |
| 104 | + vi.spyOn(archive, "removeAllDecompressedFiles").mockImplementation(() => Promise.resolve()); |
| 105 | + } |
| 106 | + const unlinkMock = vi.spyOn(fsp, "unlink").mockResolvedValue(); |
| 107 | + return [option, contents, unlinkMock]; |
| 108 | +} |
0 commit comments