|
| 1 | +import * as assert from "assert"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import * as os from "os"; |
| 5 | +import * as tar from "tar"; |
| 6 | +const AdmZip = require("adm-zip"); |
| 7 | +import * as getGGShieldUtils from "../../../lib/get-ggshield-utils"; |
| 8 | +import { ExtensionContext, window, OutputChannel } from "vscode"; |
| 9 | + |
| 10 | +suite("getGGShield integration tests", () => { |
| 11 | + let tempDir: string; |
| 12 | + let mockContext: ExtensionContext; |
| 13 | + let outputChannel: OutputChannel = window.createOutputChannel("GitGuardian"); |
| 14 | + const platform = process.platform; |
| 15 | + const arch = process.arch; |
| 16 | + const latestVersion = getGGShieldUtils.getGGShieldLatestVersion(); |
| 17 | + let originalLog: (message?: any, ...optionalParams: any[]) => void; |
| 18 | + let output: string; |
| 19 | + |
| 20 | + setup(() => { |
| 21 | + // Create temp directory for tests |
| 22 | + tempDir = fs.mkdtempSync(__dirname); |
| 23 | + mockContext = { |
| 24 | + extensionPath: tempDir, |
| 25 | + } as ExtensionContext; |
| 26 | + output = ""; // Reset captured output before each test |
| 27 | + originalLog = console.log; // Store original console.log |
| 28 | + |
| 29 | + console.log = (message: string) => { |
| 30 | + output += message; |
| 31 | + }; |
| 32 | + }); |
| 33 | + |
| 34 | + teardown(() => { |
| 35 | + if (fs.existsSync(tempDir)) { |
| 36 | + fs.rmSync(tempDir, { recursive: true }); |
| 37 | + console.log = originalLog; // Restore original console.log |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + test("returns existing binary path when binary exists", () => { |
| 42 | + const binaryPath: string = createFakeBinary( |
| 43 | + tempDir, |
| 44 | + platform, |
| 45 | + arch, |
| 46 | + latestVersion |
| 47 | + ); |
| 48 | + const result = getGGShieldUtils.getGGShield( |
| 49 | + platform, |
| 50 | + arch, |
| 51 | + mockContext, |
| 52 | + outputChannel |
| 53 | + ); |
| 54 | + |
| 55 | + assert.strictEqual(result, binaryPath); |
| 56 | + assert(fs.existsSync(result)); |
| 57 | + assert(result.includes(latestVersion)); |
| 58 | + assert( |
| 59 | + !output.includes("Updated to GGShield"), |
| 60 | + "installGGShield should not be called when binary exists" |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + test("installs binary when it doesn't exist", () => { |
| 65 | + const expectedBinaryPath: string = getGGShieldUtils.computeGGShieldPath( |
| 66 | + platform, |
| 67 | + arch, |
| 68 | + path.join(tempDir, "ggshield-internal"), |
| 69 | + latestVersion |
| 70 | + ); |
| 71 | + assert(!fs.existsSync(expectedBinaryPath)); |
| 72 | + |
| 73 | + const result = getGGShieldUtils.getGGShield( |
| 74 | + platform, |
| 75 | + arch, |
| 76 | + mockContext, |
| 77 | + outputChannel |
| 78 | + ); |
| 79 | + |
| 80 | + assert(fs.existsSync(result)); |
| 81 | + assert.strictEqual(result, expectedBinaryPath); |
| 82 | + assert(result.includes(latestVersion)); |
| 83 | + assert( |
| 84 | + !output.includes("Updated to GGShield"), |
| 85 | + "installGGShield should be called once when binary doesn't exist" |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + test("updates binary when older version exists", () => { |
| 90 | + const oldBinaryPath: string = createFakeBinary( |
| 91 | + tempDir, |
| 92 | + platform, |
| 93 | + arch, |
| 94 | + "1.0.0" |
| 95 | + ); |
| 96 | + const result = getGGShieldUtils.getGGShield( |
| 97 | + platform, |
| 98 | + arch, |
| 99 | + mockContext, |
| 100 | + outputChannel |
| 101 | + ); |
| 102 | + |
| 103 | + assert(fs.existsSync(result)); |
| 104 | + assert(result.includes(latestVersion)); |
| 105 | + assert(!fs.existsSync(oldBinaryPath)); |
| 106 | + assert( |
| 107 | + !output.includes("Updated to GGShield"), |
| 108 | + "installGGShield should be called once when updating binary" |
| 109 | + ); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +function createFakeBinary( |
| 114 | + tempDir: string, |
| 115 | + platform: NodeJS.Platform, |
| 116 | + arch: string, |
| 117 | + version: string |
| 118 | +): string { |
| 119 | + const ggshieldFolder: string = path.join(tempDir, "ggshield-internal"); |
| 120 | + const binaryName: string = platform === "win32" ? "ggshield.exe" : "ggshield"; |
| 121 | + const versionFolder: string = path.join( |
| 122 | + ggshieldFolder, |
| 123 | + `${getGGShieldUtils.computeGGShieldFolderName(platform, arch, version)}` |
| 124 | + ); |
| 125 | + const binaryPath: string = path.join(versionFolder, binaryName); |
| 126 | + fs.mkdirSync(versionFolder, { recursive: true }); |
| 127 | + fs.writeFileSync(binaryPath, "fake binary content"); |
| 128 | + return binaryPath; |
| 129 | +} |
| 130 | + |
| 131 | +suite("ggshield-resolver-utils", () => { |
| 132 | + suite("computeGGShieldPath", () => { |
| 133 | + const version = "1.0.0"; |
| 134 | + const ggshieldFolder = "/path/to/ggshield"; |
| 135 | + |
| 136 | + test("computes correct path for Windows", () => { |
| 137 | + const result = getGGShieldUtils.computeGGShieldPath( |
| 138 | + "win32", |
| 139 | + "x64", |
| 140 | + ggshieldFolder, |
| 141 | + version |
| 142 | + ); |
| 143 | + assert.strictEqual( |
| 144 | + result, |
| 145 | + path.join( |
| 146 | + ggshieldFolder, |
| 147 | + "ggshield-1.0.0-x86_64-pc-windows-msvc", |
| 148 | + "ggshield.exe" |
| 149 | + ) |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + test("computes correct path for Linux", () => { |
| 154 | + const result = getGGShieldUtils.computeGGShieldPath( |
| 155 | + "linux", |
| 156 | + "x64", |
| 157 | + ggshieldFolder, |
| 158 | + version |
| 159 | + ); |
| 160 | + assert.strictEqual( |
| 161 | + result, |
| 162 | + path.join( |
| 163 | + ggshieldFolder, |
| 164 | + "ggshield-1.0.0-x86_64-unknown-linux-gnu", |
| 165 | + "ggshield" |
| 166 | + ) |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + test("computes correct path for macOS x64", () => { |
| 171 | + const result = getGGShieldUtils.computeGGShieldPath( |
| 172 | + "darwin", |
| 173 | + "x64", |
| 174 | + ggshieldFolder, |
| 175 | + version |
| 176 | + ); |
| 177 | + assert.strictEqual( |
| 178 | + result, |
| 179 | + path.join( |
| 180 | + ggshieldFolder, |
| 181 | + "ggshield-1.0.0-x86_64-apple-darwin", |
| 182 | + "ggshield" |
| 183 | + ) |
| 184 | + ); |
| 185 | + }); |
| 186 | + |
| 187 | + test("computes correct path for macOS arm64", () => { |
| 188 | + const result = getGGShieldUtils.computeGGShieldPath( |
| 189 | + "darwin", |
| 190 | + "arm64", |
| 191 | + ggshieldFolder, |
| 192 | + version |
| 193 | + ); |
| 194 | + assert.strictEqual( |
| 195 | + result, |
| 196 | + path.join( |
| 197 | + ggshieldFolder, |
| 198 | + "ggshield-1.0.0-arm64-apple-darwin", |
| 199 | + "ggshield" |
| 200 | + ) |
| 201 | + ); |
| 202 | + }); |
| 203 | + |
| 204 | + test("throws error for unsupported platform", () => { |
| 205 | + assert.throws(() => { |
| 206 | + getGGShieldUtils.computeGGShieldPath( |
| 207 | + "sunos", |
| 208 | + "x64", |
| 209 | + ggshieldFolder, |
| 210 | + version |
| 211 | + ); |
| 212 | + }, /Unsupported platform/); |
| 213 | + }); |
| 214 | + |
| 215 | + test("throws error for unsupported architecture", () => { |
| 216 | + assert.throws(() => { |
| 217 | + getGGShieldUtils.computeGGShieldPath( |
| 218 | + "darwin", |
| 219 | + "mips", |
| 220 | + ggshieldFolder, |
| 221 | + version |
| 222 | + ); |
| 223 | + }, /Unsupported architecture/); |
| 224 | + }); |
| 225 | + }); |
| 226 | + |
| 227 | + suite("extractGGShieldBinary", () => { |
| 228 | + const testContent = "hello world"; |
| 229 | + const testFileName = "test.txt"; |
| 230 | + let tempDir: string; |
| 231 | + let tarGzPath: string; |
| 232 | + let zipPath: string; |
| 233 | + |
| 234 | + setup(() => { |
| 235 | + // Create temporary directory |
| 236 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "ggshield-test-")); |
| 237 | + |
| 238 | + // Create test file |
| 239 | + const testFilePath = path.join(tempDir, testFileName); |
| 240 | + fs.writeFileSync(testFilePath, testContent); |
| 241 | + |
| 242 | + // Create tar.gz archive |
| 243 | + tarGzPath = path.join(tempDir, "archive.tar.gz"); |
| 244 | + tar.create( |
| 245 | + { |
| 246 | + gzip: true, |
| 247 | + file: tarGzPath, |
| 248 | + cwd: tempDir, |
| 249 | + sync: true, |
| 250 | + }, |
| 251 | + [testFileName] |
| 252 | + ); |
| 253 | + |
| 254 | + // Create zip archive |
| 255 | + zipPath = path.join(tempDir, "archive.zip"); |
| 256 | + const zip = new AdmZip(); |
| 257 | + zip.addFile(testFileName, Buffer.from(testContent)); |
| 258 | + zip.writeZip(zipPath); |
| 259 | + }); |
| 260 | + |
| 261 | + teardown(() => { |
| 262 | + // Clean up temporary directory and its contents |
| 263 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 264 | + }); |
| 265 | + |
| 266 | + test("extracts tar.gz files correctly", () => { |
| 267 | + const extractDir = path.join(tempDir, "extract-tar"); |
| 268 | + fs.mkdirSync(extractDir); |
| 269 | + |
| 270 | + getGGShieldUtils.extractGGShieldBinary(tarGzPath, extractDir); |
| 271 | + |
| 272 | + const extractedContent = fs.readFileSync( |
| 273 | + path.join(extractDir, testFileName), |
| 274 | + "utf8" |
| 275 | + ); |
| 276 | + assert.strictEqual(extractedContent, testContent); |
| 277 | + }); |
| 278 | + |
| 279 | + test("extracts zip files correctly", () => { |
| 280 | + const extractDir = path.join(tempDir, "extract-zip"); |
| 281 | + fs.mkdirSync(extractDir); |
| 282 | + |
| 283 | + getGGShieldUtils.extractGGShieldBinary(zipPath, extractDir); |
| 284 | + |
| 285 | + const extractedContent = fs.readFileSync( |
| 286 | + path.join(extractDir, testFileName), |
| 287 | + "utf8" |
| 288 | + ); |
| 289 | + assert.strictEqual(extractedContent, testContent); |
| 290 | + }); |
| 291 | + |
| 292 | + test("throws error for unsupported file extension", () => { |
| 293 | + const rarPath = path.join(tempDir, "archive.rar"); |
| 294 | + fs.writeFileSync(rarPath, "unsupported file extension"); |
| 295 | + |
| 296 | + assert.throws(() => { |
| 297 | + getGGShieldUtils.extractGGShieldBinary(rarPath, tempDir); |
| 298 | + }, /Unsupported file extension/); |
| 299 | + }); |
| 300 | + }); |
| 301 | +}); |
0 commit comments