package app import ( "path/filepath" "testing" ) // These tests cover what saving says to the language server. The defect they // were written against: a window that started Untitled gains its path through // Save As, afterSave sent only didSave, and a server ignores didSave for a // document it was never told is open — so the window had no completion until // the editor was restarted. func TestSavingAnUntitledWindowAnnouncesItsDocument(t *testing.T) { project := t.TempDir() t.Chdir(project) a, _ := newTestApp(t) client, server := newFakeLanguage(t, a) connectLanguage(a, client) a.Tick() // the start-up announcement runs, and finds nothing to tell a.NewFile() typeText(a, "package main") path := filepath.Join(project, "main.go") a.save(a.ActiveView(), path) waitForMethod(t, server, "textDocument/didOpen") if got := server.methodCount("textDocument/didSave"); got != 0 { t.Errorf("didSave was sent %d times for a document the server was never told is open", got) } if !a.language.Knows(path) { t.Errorf("after the save, the server still does not know %q", path) } } func TestSaveAsUnderANewNameClosesTheOldDocument(t *testing.T) { project := t.TempDir() t.Chdir(project) a, _ := newTestApp(t) client, server := newFakeLanguage(t, a) connectLanguage(a, client) writeTestFile(t, filepath.Join(project, "old.go"), "package main\n") a.Open("old.go") waitForMethod(t, server, "textDocument/didOpen") newPath := filepath.Join(project, "new.go") a.save(a.ActiveView(), newPath) waitForMethod(t, server, "textDocument/didClose") if a.language.Knows("old.go") { t.Errorf("the old document is still open on the server after Save As gave the buffer a new name") } if !a.language.Knows(newPath) { t.Errorf("the new document was never announced to the server") } } func TestSaveAsToTheSamePathClosesNothing(t *testing.T) { // Save As offered the file's own name and the user accepted it: that is a // write, not a rename, however the dialog spelt the path. project := t.TempDir() t.Chdir(project) a, _ := newTestApp(t) client, server := newFakeLanguage(t, a) connectLanguage(a, client) writeTestFile(t, filepath.Join(project, "main.go"), "package main\n") a.Open("main.go") // relative, as the command line gives it waitForMethod(t, server, "textDocument/didOpen") a.save(a.ActiveView(), filepath.Join(project, "main.go")) // absolute, as the dialog gives it waitForMethod(t, server, "textDocument/didSave") if got := server.methodCount("textDocument/didClose"); got != 0 { t.Errorf("didClose was sent %d times for a save that renamed nothing", got) } } func TestSavingAKnownDocumentReportsTheWrite(t *testing.T) { project := t.TempDir() t.Chdir(project) a, _ := newTestApp(t) client, server := newFakeLanguage(t, a) connectLanguage(a, client) writeTestFile(t, filepath.Join(project, "main.go"), "package main\n") a.Open("main.go") waitForMethod(t, server, "textDocument/didOpen") a.save(a.ActiveView(), a.ActiveView().Buffer().Path()) waitForMethod(t, server, "textDocument/didSave") if got := server.methodCount("textDocument/didOpen"); got != 1 { t.Errorf("didOpen was sent %d times; saving an open document must not re-announce it", got) } }