Skip to content

Commit

Permalink
fix: Ensure loading of parent directory after lower level directories…
Browse files Browse the repository at this point in the history
… works (#851)

* state/documents: add failing tests

* state/documents: use dedicated index for dir lookups
  • Loading branch information
radeksimko authored Apr 6, 2022
1 parent d447050 commit adaf389
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
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

0 comments on commit adaf389

Please sign in to comment.