Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure loading of parent directory after lower level directories works #851

Merged
merged 2 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/state/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (s *DocumentStore) CloseDocument(dh document.Handle) error {

func (s *DocumentStore) ListDocumentsInDir(dirHandle document.DirHandle) ([]*document.Document, error) {
txn := s.db.Txn(false)
it, err := txn.Get(s.tableName, "id_prefix", dirHandle)
it, err := txn.Get(s.tableName, "dir", dirHandle)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func (s *DocumentStore) IsDocumentOpen(dh document.Handle) (bool, error) {
func (s *DocumentStore) HasOpenDocuments(dirHandle document.DirHandle) (bool, error) {
txn := s.db.Txn(false)

obj, err := txn.First(s.tableName, "id_prefix", dirHandle)
obj, err := txn.First(s.tableName, "dir", dirHandle)
if err != nil {
return false, err
}
Expand Down
65 changes: 65 additions & 0 deletions internal/state/documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,48 @@ func TestDocumentStore_ListDocumentsInDir(t *testing.T) {
}
}

func TestDocumentStore_ListDocumentsInDir_parentDir(t *testing.T) {
s, err := NewStateStore()
if err != nil {
t.Fatal(err)
}

s.DocumentStore.TimeProvider = testTimeProvider

testHandle1 := document.HandleFromURI("file:///dir/test1.tf")
err = s.DocumentStore.OpenDocument(testHandle1, "terraform", 0, []byte("foobar"))
if err != nil {
t.Fatal(err)
}

testHandle2 := document.HandleFromURI("file:///dir/sub/test2.tf")
err = s.DocumentStore.OpenDocument(testHandle2, "terraform", 0, []byte("foobar"))
if err != nil {
t.Fatal(err)
}

dirHandle := document.DirHandleFromURI("file:///dir")
docs, err := s.DocumentStore.ListDocumentsInDir(dirHandle)
if err != nil {
t.Fatal(err)
}

expectedDocs := []*document.Document{
{
Dir: dirHandle,
Filename: "test1.tf",
ModTime: testTimeProvider(),
LanguageID: "terraform",
Version: 0,
Text: []byte("foobar"),
Lines: source.MakeSourceLines("test1.tf", []byte("foobar")),
},
}
if diff := cmp.Diff(expectedDocs, docs); diff != "" {
t.Fatalf("unexpected docs: %s", diff)
}
}

func TestDocumentStore_IsDocumentOpen(t *testing.T) {
s, err := NewStateStore()
if err != nil {
Expand Down Expand Up @@ -250,7 +292,30 @@ func TestDocumentStore_HasOpenDocuments(t *testing.T) {
if hasOpenDocs {
t.Fatal("expected to find no open documents")
}
}

func TestDocumentStore_HasOpenDocuments_parentDir(t *testing.T) {
s, err := NewStateStore()
if err != nil {
t.Fatal(err)
}

s.DocumentStore.TimeProvider = testTimeProvider

testHandle := document.HandleFromURI("file:///dir/sub/test2.tf")
err = s.DocumentStore.OpenDocument(testHandle, "terraform", 0, []byte("foobar"))
if err != nil {
t.Fatal(err)
}

dirHandle := document.DirHandleFromURI("file:///dir")
hasOpenDocs, err := s.DocumentStore.HasOpenDocuments(dirHandle)
if err != nil {
t.Fatal(err)
}
if hasOpenDocs {
t.Fatal("expected to find no open documents")
}
}

func testTimeProvider() time.Time {
Expand Down
2 changes: 1 addition & 1 deletion internal/state/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (js *JobStore) FinishJob(id job.ID, jobErr error, deferredJobIds ...job.ID)
return fmt.Errorf("failed to copy a job: %w", err)
}

js.logger.Printf("JOBS: Finishing job %q: %q for %q (err = %#v, deferredJobs: %q)",
js.logger.Printf("JOBS: Finishing job %q: %q for %q (err = %s, deferredJobs: %q)",
sj.ID, sj.Type, sj.Dir, jobErr, deferredJobIds)

_, err = txn.DeleteAll(js.tableName, "id", id)
Expand Down
4 changes: 4 additions & 0 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ var dbSchema = &memdb.DBSchema{
},
},
},
"dir": {
Name: "dir",
Indexer: &DirHandleFieldIndexer{Field: "Dir"},
},
},
},
jobsTableName: {
Expand Down