depsfile: Allow loading locks from a byte array in memory

This won't be a typical usage pattern for normal code, but will be useful
for tests that need to work with locks as input so that they don't need to
write out a temporary file on disk just to read it back in immediately.
This commit is contained in:
Martin Atkins 2020-10-20 14:53:38 -07:00
parent 1e32354e3e
commit fc5a41b5e5
1 changed files with 21 additions and 1 deletions

View File

@ -31,12 +31,32 @@ import (
// If the returned diagnostics contains errors then the returned Locks may
// be incomplete or invalid.
func LoadLocksFromFile(filename string) (*Locks, tfdiags.Diagnostics) {
return loadLocks(func(parser *hclparse.Parser) (*hcl.File, hcl.Diagnostics) {
return parser.ParseHCLFile(filename)
})
}
// LoadLocksFromBytes reads locks from the given byte array, pretending that
// it was read from the given filename.
//
// The constraints and behaviors are otherwise the same as for
// LoadLocksFromFile. LoadLocksFromBytes is primarily to allow more convenient
// integration testing (avoiding creating temporary files on disk); if you
// are writing non-test code, consider whether LoadLocksFromFile might be
// more appropriate to call.
func LoadLocksFromBytes(src []byte, filename string) (*Locks, tfdiags.Diagnostics) {
return loadLocks(func(parser *hclparse.Parser) (*hcl.File, hcl.Diagnostics) {
return parser.ParseHCL(src, filename)
})
}
func loadLocks(loadParse func(*hclparse.Parser) (*hcl.File, hcl.Diagnostics)) (*Locks, tfdiags.Diagnostics) {
ret := NewLocks()
var diags tfdiags.Diagnostics
parser := hclparse.NewParser()
f, hclDiags := parser.ParseHCLFile(filename)
f, hclDiags := loadParse(parser)
ret.sources = parser.Sources()
diags = diags.Append(hclDiags)