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

Add File() #1945

Merged
merged 3 commits into from
Jul 7, 2024
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
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
Loading