|
| 1 | +importfsfrom"fs/promises"; |
| 2 | +importosfrom"os"; |
| 3 | +importpathfrom"path"; |
| 4 | +import{beforeAll,describe,expect,it}from"vitest"; |
| 5 | +import*asclifrom"./core/cliUtils"; |
| 6 | + |
| 7 | +describe("cliUtils",()=>{ |
| 8 | +consttmp=path.join(os.tmpdir(),"vscode-coder-tests"); |
| 9 | + |
| 10 | +beforeAll(async()=>{ |
| 11 | +// Clean up from previous tests, if any. |
| 12 | +awaitfs.rm(tmp,{recursive:true,force:true}); |
| 13 | +awaitfs.mkdir(tmp,{recursive:true}); |
| 14 | +}); |
| 15 | + |
| 16 | +it("name",()=>{ |
| 17 | +expect(cli.name().startsWith("coder-")).toBeTruthy(); |
| 18 | +}); |
| 19 | + |
| 20 | +it("stat",async()=>{ |
| 21 | +constbinPath=path.join(tmp,"stat"); |
| 22 | +expect(awaitcli.stat(binPath)).toBeUndefined(); |
| 23 | + |
| 24 | +awaitfs.writeFile(binPath,"test"); |
| 25 | +expect((awaitcli.stat(binPath))?.size).toBe(4); |
| 26 | +}); |
| 27 | + |
| 28 | +// TODO: CI only runs on Linux but we should run it on Windows too. |
| 29 | +it("version",async()=>{ |
| 30 | +constbinPath=path.join(tmp,"version"); |
| 31 | +awaitexpect(cli.version(binPath)).rejects.toThrow("ENOENT"); |
| 32 | + |
| 33 | +constbinTmpl=awaitfs.readFile( |
| 34 | +path.join(__dirname,"../fixtures/bin.bash"), |
| 35 | +"utf8", |
| 36 | +); |
| 37 | +awaitfs.writeFile(binPath,binTmpl.replace("$ECHO","hello")); |
| 38 | +awaitexpect(cli.version(binPath)).rejects.toThrow("EACCES"); |
| 39 | + |
| 40 | +awaitfs.chmod(binPath,"755"); |
| 41 | +awaitexpect(cli.version(binPath)).rejects.toThrow("Unexpected token"); |
| 42 | + |
| 43 | +awaitfs.writeFile(binPath,binTmpl.replace("$ECHO","{}")); |
| 44 | +awaitexpect(cli.version(binPath)).rejects.toThrow( |
| 45 | +"No version found in output", |
| 46 | +); |
| 47 | + |
| 48 | +awaitfs.writeFile( |
| 49 | +binPath, |
| 50 | +binTmpl.replace( |
| 51 | +"$ECHO", |
| 52 | +JSON.stringify({ |
| 53 | +version:"v0.0.0", |
| 54 | +}), |
| 55 | +), |
| 56 | +); |
| 57 | +expect(awaitcli.version(binPath)).toBe("v0.0.0"); |
| 58 | + |
| 59 | +constoldTmpl=awaitfs.readFile( |
| 60 | +path.join(__dirname,"../fixtures/bin.old.bash"), |
| 61 | +"utf8", |
| 62 | +); |
| 63 | +constold=(stderr:string,stdout:string):string=>{ |
| 64 | +returnoldTmpl.replace("$STDERR",stderr).replace("$STDOUT",stdout); |
| 65 | +}; |
| 66 | + |
| 67 | +// Should fall back only if it says "unknown flag". |
| 68 | +awaitfs.writeFile(binPath,old("foobar","Coder v1.1.1")); |
| 69 | +awaitexpect(cli.version(binPath)).rejects.toThrow("foobar"); |
| 70 | + |
| 71 | +awaitfs.writeFile(binPath,old("unknown flag: --output","Coder v1.1.1")); |
| 72 | +expect(awaitcli.version(binPath)).toBe("v1.1.1"); |
| 73 | + |
| 74 | +// Should trim off the newline if necessary. |
| 75 | +awaitfs.writeFile( |
| 76 | +binPath, |
| 77 | +old("unknown flag: --output\n","Coder v1.1.1\n"), |
| 78 | +); |
| 79 | +expect(awaitcli.version(binPath)).toBe("v1.1.1"); |
| 80 | + |
| 81 | +// Error with original error if it does not begin with "Coder". |
| 82 | +awaitfs.writeFile(binPath,old("unknown flag: --output","Unrelated")); |
| 83 | +awaitexpect(cli.version(binPath)).rejects.toThrow("unknown flag"); |
| 84 | + |
| 85 | +// Error if no version. |
| 86 | +awaitfs.writeFile(binPath,old("unknown flag: --output","Coder")); |
| 87 | +awaitexpect(cli.version(binPath)).rejects.toThrow("No version found"); |
| 88 | +}); |
| 89 | + |
| 90 | +it("rmOld",async()=>{ |
| 91 | +constbinDir=path.join(tmp,"bins"); |
| 92 | +expect(awaitcli.rmOld(path.join(binDir,"bin1"))).toStrictEqual([]); |
| 93 | + |
| 94 | +awaitfs.mkdir(binDir,{recursive:true}); |
| 95 | +awaitfs.writeFile(path.join(binDir,"bin.old-1"),"echo hello"); |
| 96 | +awaitfs.writeFile(path.join(binDir,"bin.old-2"),"echo hello"); |
| 97 | +awaitfs.writeFile(path.join(binDir,"bin.temp-1"),"echo hello"); |
| 98 | +awaitfs.writeFile(path.join(binDir,"bin.temp-2"),"echo hello"); |
| 99 | +awaitfs.writeFile(path.join(binDir,"bin1"),"echo hello"); |
| 100 | +awaitfs.writeFile(path.join(binDir,"bin2"),"echo hello"); |
| 101 | +awaitfs.writeFile(path.join(binDir,"bin.asc"),"echo hello"); |
| 102 | +awaitfs.writeFile(path.join(binDir,"bin.old-1.asc"),"echo hello"); |
| 103 | +awaitfs.writeFile(path.join(binDir,"bin.temp-2.asc"),"echo hello"); |
| 104 | + |
| 105 | +expect(awaitcli.rmOld(path.join(binDir,"bin1"))).toStrictEqual([ |
| 106 | +{ |
| 107 | +fileName:"bin.asc", |
| 108 | +error:undefined, |
| 109 | +}, |
| 110 | +{ |
| 111 | +fileName:"bin.old-1", |
| 112 | +error:undefined, |
| 113 | +}, |
| 114 | +{ |
| 115 | +fileName:"bin.old-1.asc", |
| 116 | +error:undefined, |
| 117 | +}, |
| 118 | +{ |
| 119 | +fileName:"bin.old-2", |
| 120 | +error:undefined, |
| 121 | +}, |
| 122 | +{ |
| 123 | +fileName:"bin.temp-1", |
| 124 | +error:undefined, |
| 125 | +}, |
| 126 | +{ |
| 127 | +fileName:"bin.temp-2", |
| 128 | +error:undefined, |
| 129 | +}, |
| 130 | +{ |
| 131 | +fileName:"bin.temp-2.asc", |
| 132 | +error:undefined, |
| 133 | +}, |
| 134 | +]); |
| 135 | + |
| 136 | +expect(awaitfs.readdir(path.join(tmp,"bins"))).toStrictEqual([ |
| 137 | +"bin1", |
| 138 | +"bin2", |
| 139 | +]); |
| 140 | +}); |
| 141 | + |
| 142 | +it("ETag",async()=>{ |
| 143 | +constbinPath=path.join(tmp,"hash"); |
| 144 | + |
| 145 | +awaitfs.writeFile(binPath,"foobar"); |
| 146 | +expect(awaitcli.eTag(binPath)).toBe( |
| 147 | +"8843d7f92416211de9ebb963ff4ce28125932878", |
| 148 | +); |
| 149 | + |
| 150 | +awaitfs.writeFile(binPath,"test"); |
| 151 | +expect(awaitcli.eTag(binPath)).toBe( |
| 152 | +"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", |
| 153 | +); |
| 154 | +}); |
| 155 | +}); |