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

depsfile: Don't panic when lock file is unreadable #27250

Merged
merged 1 commit into from
Dec 16, 2020
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
6 changes: 6 additions & 0 deletions internal/depsfile/locks_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func loadLocks(loadParse func(*hclparse.Parser) (*hcl.File, hcl.Diagnostics)) (*
f, hclDiags := loadParse(parser)
ret.sources = parser.Sources()
diags = diags.Append(hclDiags)
if f == nil {
// If we encountered an error loading the file then those errors
// should already be in diags from the above, but the file might
// also be nil itself and so we can't decode from it.
return ret, diags
}

moreDiags := decodeLocksFromHCL(ret, f.Body)
diags = diags.Append(moreDiags)
Expand Down
39 changes: 39 additions & 0 deletions internal/depsfile/locks_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,45 @@ func TestLoadLocksFromFile(t *testing.T) {
}
}

func TestLoadLocksFromFileAbsent(t *testing.T) {
t.Run("lock file is a directory", func(t *testing.T) {
// This can never happen when Terraform is the one generating the
// lock file, but might arise if the user makes a directory with the
// lock file's name for some reason. (There is no actual reason to do
// so, so that would always be a mistake.)
locks, diags := LoadLocksFromFile("testdata")
if len(locks.providers) != 0 {
t.Errorf("returned locks has providers; expected empty locks")
}
if !diags.HasErrors() {
t.Fatalf("LoadLocksFromFile succeeded; want error")
}
// This is a generic error message from HCL itself, so upgrading HCL
// in future might cause a different error message here.
want := `Failed to read file: The configuration file "testdata" could not be read.`
got := diags.Err().Error()
if got != want {
t.Errorf("wrong error message\ngot: %s\nwant: %s", got, want)
}
})
t.Run("lock file doesn't exist", func(t *testing.T) {
locks, diags := LoadLocksFromFile("testdata/nonexist.hcl")
if len(locks.providers) != 0 {
t.Errorf("returned locks has providers; expected empty locks")
}
if !diags.HasErrors() {
t.Fatalf("LoadLocksFromFile succeeded; want error")
}
// This is a generic error message from HCL itself, so upgrading HCL
// in future might cause a different error message here.
want := `Failed to read file: The configuration file "testdata/nonexist.hcl" could not be read.`
got := diags.Err().Error()
if got != want {
t.Errorf("wrong error message\ngot: %s\nwant: %s", got, want)
}
})
}

func TestSaveLocksToFile(t *testing.T) {
locks := NewLocks()

Expand Down