Skip to content

Commit 794d9fa

Browse files
committed
chore: merge
2 parents 607480b + bdeddc7 commit 794d9fa

File tree

7 files changed

+35
-29
lines changed

7 files changed

+35
-29
lines changed

lib/analyzer/applications/node-modules-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Debug from "debug";
22
import { mkdir, mkdtemp, rm, stat, writeFile } from "fs/promises";
3+
import * as os from "os";
34
import * as path from "path";
45
import { FilePathToContent, FilesByDirMap } from "./types";
56
const debug = Debug("snyk");
@@ -22,7 +23,7 @@ interface ScanPaths {
2223
async function createTempProjectDir(
2324
projectDir: string,
2425
): Promise<{ tmpDir: string; tempProjectRoot: string }> {
25-
const tmpDir = await mkdtemp("snyk");
26+
const tmpDir = await mkdtemp(path.join(os.tmpdir(), "snyk-"));
2627

2728
const tempProjectRoot = path.join(tmpDir, projectDir);
2829

lib/analyzer/image-inspector.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as Debug from "debug";
22
import * as fs from "fs";
3-
import * as mkdirp from "mkdirp";
43
import * as path from "path";
54

65
import { Docker, DockerOptions } from "../docker";
@@ -200,7 +199,7 @@ async function getImageArchive(
200199
platform?: string,
201200
): Promise<ArchiveResult> {
202201
const docker = new Docker();
203-
mkdirp.sync(imageSavePath);
202+
fs.mkdirSync(imageSavePath, { recursive: true });
204203
const destination: DestinationDir = {
205204
name: imageSavePath,
206205
removeCallback: cleanupCallback(imageSavePath, "image.tar"),

lib/image-save-path.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import * as crypto from "crypto";
2+
import * as os from "os";
13
import * as path from "path";
2-
import * as tmp from "tmp";
3-
import { v4 as uuidv4 } from "uuid";
44

55
export function fullImageSavePath(imageSavePath: string | undefined): string {
6-
let imagePath = tmp.dirSync().name;
6+
let imagePath = os.tmpdir();
77
if (imageSavePath) {
88
imagePath = path.normalize(imageSavePath);
99
}
1010

11-
return path.join(imagePath, uuidv4());
11+
return path.join(imagePath, crypto.randomUUID());
1212
}

package-lock.json

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,14 @@
4646
"fzstd": "^0.1.1",
4747
"gunzip-maybe": "^1.4.2",
4848
"minimatch": "^9.0.0",
49-
"mkdirp": "^1.0.4",
5049
"packageurl-js": "1.2.0",
5150
"semver": "^7.7.3",
5251
"shescape": "^2.1.7",
5352
"snyk-nodejs-lockfile-parser": "^2.7.0",
5453
"snyk-poetry-lockfile-parser": "1.9.1",
5554
"snyk-resolve-deps": "^4.9.1",
5655
"tar-stream": "^2.2.0",
57-
"tmp": "^0.2.5",
5856
"tslib": "^1",
59-
"uuid": "^8.2.0",
6057
"varint": "^6.0.0"
6158
},
6259
"devDependencies": {

test/system/docker.spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
import * as os from "os";
1111
import * as path from "path";
1212
import * as tar from "tar-stream";
13-
import * as tmp from "tmp";
1413
import { Docker } from "../../lib/docker";
1514
import { CmdOutput } from "../../lib/sub-process";
1615
import * as subProcess from "../../lib/sub-process";
@@ -123,6 +122,7 @@ describe("docker", () => {
123122

124123
const docker = new Docker();
125124
let expectedChecksum;
125+
let tempFilesToCleanup: string[] = [];
126126

127127
beforeAll(async () => {
128128
const loadImage = path.join(
@@ -139,6 +139,12 @@ describe("docker", () => {
139139
if (existsSync(TEST_TARGET_IMAGE_DESTINATION)) {
140140
unlinkSync(TEST_TARGET_IMAGE_DESTINATION);
141141
}
142+
for (const file of tempFilesToCleanup) {
143+
if (existsSync(file)) {
144+
unlinkSync(file);
145+
}
146+
}
147+
tempFilesToCleanup = [];
142148
});
143149

144150
async function calculateImageSHA256(tarFilePath: string): Promise<string> {
@@ -164,9 +170,12 @@ describe("docker", () => {
164170
return new Promise((resolve, reject) => {
165171
const extract = tar.extract();
166172
const pack = tar.pack();
167-
const tempFile = tmp.fileSync();
168-
const output = createWriteStream(tempFile.name);
169-
173+
const tempFilePath = path.join(
174+
os.tmpdir(),
175+
`snyk-docker-plugin-test-${crypto.randomUUID()}.tar`,
176+
);
177+
tempFilesToCleanup.push(tempFilePath);
178+
const output = createWriteStream(tempFilePath);
170179
extract.on("entry", (header, stream, next) => {
171180
// Normalize the header
172181
header.mtime = new Date(0); // Set modification time to the epoch
@@ -183,7 +192,7 @@ describe("docker", () => {
183192
});
184193

185194
output.on("finish", () => {
186-
resolve(tempFile.name);
195+
resolve(tempFilePath);
187196
});
188197

189198
extract.on("error", (err) => {

test/system/image-inspector.spec.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import * as crypto from "crypto";
12
import * as fs from "fs";
3+
import * as os from "os";
24
import * as path from "path";
3-
import * as tmp from "tmp";
4-
import { v4 as uuidv4 } from "uuid";
55

66
import { DockerPullResult } from "@snyk/snyk-docker-pull";
77
import * as plugin from "../../lib";
@@ -114,7 +114,7 @@ describe("getImageArchive", () => {
114114
});
115115

116116
it("should produce the expected state", async () => {
117-
const imageSavePath = path.join(customPath, uuidv4());
117+
const imageSavePath = path.join(customPath, crypto.randomUUID());
118118
const dockerPullSpy = jest.spyOn(Docker.prototype, "pull");
119119
const loadImage = path.join(
120120
__dirname,
@@ -152,8 +152,8 @@ describe("getImageArchive", () => {
152152

153153
describe("from remote registry with binary", () => {
154154
it("should produce the expected state", async () => {
155-
const customPath = tmp.dirSync().name;
156-
const imageSavePath = path.join(customPath, uuidv4());
155+
const customPath = fs.mkdtempSync(path.join(os.tmpdir(), "snyk-"));
156+
const imageSavePath = path.join(customPath, crypto.randomUUID());
157157
const registryPullSpy = jest.spyOn(Docker.prototype, "pull");
158158

159159
const archiveLocation: ArchiveResult =
@@ -180,11 +180,12 @@ describe("getImageArchive", () => {
180180
expect(customPathExistsOnDisk).toBe(true);
181181

182182
await subProcess.execute("docker", ["image", "rm", targetImage]);
183+
fs.rmSync(customPath, { recursive: true, force: true });
183184
});
184185

185186
it("should fail correctly when manifest is not found for given tag", async () => {
186-
const customPath = tmp.dirSync().name;
187-
const imageSavePath = path.join(customPath, uuidv4());
187+
const customPath = fs.mkdtempSync(path.join(os.tmpdir(), "snyk-"));
188+
const imageSavePath = path.join(customPath, crypto.randomUUID());
188189
const dockerPullCliSpy = jest
189190
.spyOn(Docker.prototype, "pullCli")
190191
.mockImplementation(() => {
@@ -209,6 +210,8 @@ describe("getImageArchive", () => {
209210

210211
expect(dockerPullCliSpy).toHaveBeenCalled();
211212
expect(dockerPullSpy).not.toHaveBeenCalled();
213+
214+
fs.rmSync(customPath, { recursive: true, force: true });
212215
});
213216
});
214217

@@ -220,7 +223,7 @@ describe("getImageArchive", () => {
220223
});
221224

222225
it("should produce the expected state", async () => {
223-
const imageSavePath = path.join(customPath, uuidv4());
226+
const imageSavePath = path.join(customPath, crypto.randomUUID());
224227
// we simulate the Docker CLI being so old that the `--platform` flag is not supported at all.
225228
const dockerPullCliSpy = jest
226229
.spyOn(Docker.prototype, "pullCli")
@@ -258,7 +261,7 @@ describe("getImageArchive", () => {
258261
});
259262

260263
it("should produce the expected state", async () => {
261-
const imageSavePath = path.join(customPath, uuidv4());
264+
const imageSavePath = path.join(customPath, crypto.randomUUID());
262265
const dockerPullCliSpy = jest
263266
.spyOn(Docker.prototype, "pullCli")
264267
.mockImplementation(() => {
@@ -296,7 +299,7 @@ describe("getImageArchive", () => {
296299
});
297300

298301
it("should produce the expected state", async () => {
299-
const imageSavePath = path.join(customPath, uuidv4());
302+
const imageSavePath = path.join(customPath, crypto.randomUUID());
300303
const dockerPullSpy = jest.spyOn(Docker.prototype, "pull");
301304
jest.spyOn(subProcess, "execute").mockImplementation(() => {
302305
throw new Error();
@@ -334,7 +337,7 @@ describe("getImageArchive", () => {
334337
});
335338

336339
it("should produce the expected state", async () => {
337-
const imageSavePath = path.join(customPath, uuidv4());
340+
const imageSavePath = path.join(customPath, crypto.randomUUID());
338341
const dockerPullSpy = jest
339342
.spyOn(Docker.prototype, "pull")
340343
.mockImplementation((_1, _2, _3, imageSavePath) => {

0 commit comments

Comments
 (0)