1namespace ts.projectSystem { 2 describe("unittests:: tsserver:: reload", () => { 3 it("should work with temp file", () => { 4 const f1 = { 5 path: "/a/b/app.ts", 6 content: "let x = 1" 7 }; 8 const tmp = { 9 path: "/a/b/app.tmp", 10 content: "const y = 42" 11 }; 12 const host = createServerHost([f1, tmp]); 13 const session = createSession(host); 14 15 // send open request 16 session.executeCommand({ 17 type: "request", 18 command: "open", 19 seq: 1, 20 arguments: { file: f1.path } 21 } as server.protocol.OpenRequest); 22 23 // reload from tmp file 24 session.executeCommand({ 25 type: "request", 26 command: "reload", 27 seq: 2, 28 arguments: { file: f1.path, tmpfile: tmp.path } 29 } as server.protocol.ReloadRequest); 30 31 // verify content 32 const projectServiice = session.getProjectService(); 33 const snap1 = projectServiice.getScriptInfo(f1.path)!.getSnapshot(); 34 assert.equal(getSnapshotText(snap1), tmp.content, "content should be equal to the content of temp file"); 35 36 // reload from original file file 37 session.executeCommand({ 38 type: "request", 39 command: "reload", 40 seq: 2, 41 arguments: { file: f1.path } 42 } as server.protocol.ReloadRequest); 43 44 // verify content 45 const snap2 = projectServiice.getScriptInfo(f1.path)!.getSnapshot(); 46 assert.equal(getSnapshotText(snap2), f1.content, "content should be equal to the content of original file"); 47 48 }); 49 50 it("should work when script info doesnt have any project open", () => { 51 const f1 = { 52 path: "/a/b/app.ts", 53 content: "let x = 1" 54 }; 55 const tmp = { 56 path: "/a/b/app.tmp", 57 content: "const y = 42" 58 }; 59 const host = createServerHost([f1, tmp, libFile]); 60 const session = createSession(host); 61 const openContent = "let z = 1"; 62 // send open request 63 session.executeCommandSeq({ 64 command: server.protocol.CommandTypes.Open, 65 arguments: { file: f1.path, fileContent: openContent } 66 } as server.protocol.OpenRequest); 67 68 const projectService = session.getProjectService(); 69 checkNumberOfProjects(projectService, { inferredProjects: 1 }); 70 const info = projectService.getScriptInfo(f1.path)!; 71 assert.isDefined(info); 72 checkScriptInfoContents(openContent, "contents set during open request"); 73 74 // send close request 75 session.executeCommandSeq({ 76 command: server.protocol.CommandTypes.Close, 77 arguments: { file: f1.path } 78 } as server.protocol.CloseRequest); 79 checkScriptInfoAndProjects(f1.content, "contents of closed file"); 80 checkInferredProjectIsOrphan(); 81 82 // Can reload contents of the file when its not open and has no project 83 // reload from temp file 84 session.executeCommandSeq({ 85 command: server.protocol.CommandTypes.Reload, 86 arguments: { file: f1.path, tmpfile: tmp.path } 87 } as server.protocol.ReloadRequest); 88 checkScriptInfoAndProjects(tmp.content, "contents of temp file"); 89 checkInferredProjectIsOrphan(); 90 91 // reload from own file 92 session.executeCommandSeq({ 93 command: server.protocol.CommandTypes.Reload, 94 arguments: { file: f1.path } 95 } as server.protocol.ReloadRequest); 96 checkScriptInfoAndProjects(f1.content, "contents of closed file"); 97 checkInferredProjectIsOrphan(); 98 99 // Open file again without setting its content 100 session.executeCommandSeq({ 101 command: server.protocol.CommandTypes.Open, 102 arguments: { file: f1.path } 103 } as server.protocol.OpenRequest); 104 checkScriptInfoAndProjects(f1.content, "contents of file when opened without specifying contents"); 105 const snap = info.getSnapshot(); 106 107 // send close request 108 session.executeCommandSeq({ 109 command: server.protocol.CommandTypes.Close, 110 arguments: { file: f1.path } 111 } as server.protocol.CloseRequest); 112 checkScriptInfoAndProjects(f1.content, "contents of closed file"); 113 assert.strictEqual(info.getSnapshot(), snap); 114 checkInferredProjectIsOrphan(); 115 116 // reload from temp file 117 session.executeCommandSeq({ 118 command: server.protocol.CommandTypes.Reload, 119 arguments: { file: f1.path, tmpfile: tmp.path } 120 } as server.protocol.ReloadRequest); 121 checkScriptInfoAndProjects(tmp.content, "contents of temp file"); 122 assert.notStrictEqual(info.getSnapshot(), snap); 123 checkInferredProjectIsOrphan(); 124 125 // reload from own file 126 session.executeCommandSeq({ 127 command: server.protocol.CommandTypes.Reload, 128 arguments: { file: f1.path } 129 } as server.protocol.ReloadRequest); 130 checkScriptInfoAndProjects(f1.content, "contents of closed file"); 131 assert.notStrictEqual(info.getSnapshot(), snap); 132 checkInferredProjectIsOrphan(); 133 134 function checkInferredProjectIsOrphan() { 135 assert.isTrue(projectService.inferredProjects[0].isOrphan()); 136 assert.equal(info.containingProjects.length, 0); 137 } 138 139 function checkScriptInfoAndProjects(contentsOfInfo: string, captionForContents: string) { 140 checkNumberOfProjects(projectService, { inferredProjects: 1 }); 141 assert.strictEqual(projectService.getScriptInfo(f1.path), info); 142 checkScriptInfoContents(contentsOfInfo, captionForContents); 143 } 144 145 function checkScriptInfoContents(contentsOfInfo: string, captionForContents: string) { 146 const snap = info.getSnapshot(); 147 assert.equal(getSnapshotText(snap), contentsOfInfo, "content should be equal to " + captionForContents); 148 } 149 }); 150 }); 151} 152