Skip to content

Commit

Permalink
Merge pull request #1945 from 6543-forks/File_ValueSource
Browse files Browse the repository at this point in the history
Add File()
  • Loading branch information
abennett authored Jul 7, 2024
2 parents c25f563 + bb1b3a9 commit 6d6416e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,8 @@ type ValueSource interface {

func EnvVar(key string) ValueSource

func File(path string) ValueSource

type ValueSourceChain struct {
Chain []ValueSource
}
Expand Down
2 changes: 2 additions & 0 deletions testdata/godoc-v3.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,8 @@ type ValueSource interface {

func EnvVar(key string) ValueSource

func File(path string) ValueSource

type ValueSourceChain struct {
Chain []ValueSource
}
Expand Down
4 changes: 4 additions & 0 deletions value_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ func (f *fileValueSource) GoString() string {
return fmt.Sprintf("&fileValueSource{Path:%[1]q}", f.Path)
}

func File(path string) ValueSource {
return &fileValueSource{Path: path}
}

// Files is a helper function to encapsulate a number of
// fileValueSource together as a ValueSourceChain
func Files(paths ...string) ValueSourceChain {
Expand Down
8 changes: 4 additions & 4 deletions value_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestFileValueSource(t *testing.T) {
r.Implements((*ValueSource)(nil), &fileValueSource{})

t.Run("not found", func(t *testing.T) {
src := &fileValueSource{Path: fmt.Sprintf("junk_file_name-%[1]v", rand.Int())}
src := File(fmt.Sprintf("junk_file_name-%[1]v", rand.Int()))
_, ok := src.Lookup()
r.False(ok)
})
Expand All @@ -82,23 +82,23 @@ func TestFileValueSource(t *testing.T) {
r.Nil(os.WriteFile(fileName, []byte("pita"), 0o644))

t.Run("found", func(t *testing.T) {
src := &fileValueSource{Path: fileName}
src := File(fileName)
str, ok := src.Lookup()
r.True(ok)
r.Equal("pita", str)
})
})

t.Run("implements fmt.Stringer", func(t *testing.T) {
src := &fileValueSource{Path: "/dev/null"}
src := File("/dev/null")
r := require.New(t)

r.Implements((*ValueSource)(nil), src)
r.Equal("file \"/dev/null\"", src.String())
})

t.Run("implements fmt.GoStringer", func(t *testing.T) {
src := &fileValueSource{Path: "/dev/null"}
src := File("/dev/null")
r := require.New(t)

r.Implements((*ValueSource)(nil), src)
Expand Down

0 comments on commit 6d6416e

Please sign in to comment.