Skip to content

Commit

Permalink
FIX: pricePerBroadcaster JSON file line breaks parsing bug (#2914)
Browse files Browse the repository at this point in the history
* FIX: pricePerBroadcaster JSON file line breaks parsing bug

---------

Co-authored-by: Rick Staa <[email protected]>
  • Loading branch information
thomshutt and rickstaa authored Dec 8, 2023
1 parent 5b69cd6 commit fdda451
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

### Bug Fixes 🐞

- \[#2914](https://github.com/livepeer/go-livepeer/issues/2912) fixes a bug that prevented `pricePerBroadcaster` JSON files with line-breaks from being parsed correctly (@rickstaa).

#### CLI

#### General
Expand Down
19 changes: 6 additions & 13 deletions common/readfromfile.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package common

import (
"bufio"
"fmt"
"os"
"strings"
)

// ReadFromFile attempts to read a file at the supplied location.
Expand All @@ -16,25 +16,18 @@ func ReadFromFile(s string) (string, error) {
return s, err
}
if info.IsDir() {
// If the supplied string is a directory,
// assume it is the pass and return it
// along with an approptiate error.
// If the supplied string is a directory, return it along with an appropriate error.
return s, fmt.Errorf("supplied path is a directory")
}
file, err := os.Open(s)
bytes, err := os.ReadFile(s)
if err != nil {
return s, err
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
txt := strings.TrimSpace(string(bytes))

scanner.Scan()
txtline := scanner.Text()
file.Close()

if len(txtline) == 0 {
if len(txt) <= 0 {
return s, fmt.Errorf("supplied file is empty")
}

return txtline, nil
return txt, nil
}
23 changes: 19 additions & 4 deletions common/readfromfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,29 @@ func TestReadFromFile_FileExistsOneLine(t *testing.T) {
assert.Equal(expectedOutput, output)
}

// Check that we can reliably read a single line value from the file, even when it ends in a newline / whitespace
func TestReadFromFile_FileExistsEndsInNewline(t *testing.T) {
input :=
`something
`
f, err := os.CreateTemp(os.TempDir(), "TestReadFromFile_FileExistsEndsInNewline*")
require.NoError(t, err)
defer os.Remove(f.Name())

_, err = f.WriteString(input)
require.NoError(t, err)

output, err := ReadFromFile(f.Name())
require.NoError(t, err)
require.Equal(t, "something", output)
}

func TestReadFromFile_FileExistsMultiline(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

input :=
`something
somethingelse`
expectedOutput := "something"

tmpFile := "TestReadFromFile_FileExistsMultiline.txt"

Expand All @@ -112,7 +127,7 @@ somethingelse`

output, err := ReadFromFile(tmpFile)

assert.Nil(err)
require.NoError(err)
// ReadFromFile should the first line of the text file
assert.Equal(expectedOutput, output)
require.Equal(input, output)
}

0 comments on commit fdda451

Please sign in to comment.