Skip to content

Commit

Permalink
Truncate output file/dir before the export
Browse files Browse the repository at this point in the history
  • Loading branch information
amishas157 committed Aug 6, 2024
1 parent 687ff9d commit 8ea6e38
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions cmd/export_ledgers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,39 @@ func runCLITest(t *testing.T, test cliTest, goldenFolder string) {
dir, err := os.Getwd()
assert.NoError(t, err)

idxOfOutputArg := indexOf(test.args, "-o")
var testOutput []byte
var outLocation string
var stat os.FileInfo
if idxOfOutputArg > -1 {
outLocation = test.args[idxOfOutputArg+1]
stat, err = os.Stat(outLocation)
if err != nil {
// Check if the error is due to the file not existing
if !os.IsNotExist(err) {
assert.NoError(t, err)
}
} else {
if stat.IsDir() {
err := os.Remove(outLocation)
if err != nil {
log.Fatal(err)
}
} else {
_, err = clearOutputFile(outLocation)
if err != nil {
log.Fatal(err)
}
}
}
}

cmd := exec.Command(path.Join(dir, executableName), test.args...)
errOut, actualError := cmd.CombinedOutput()

idxOfOutputArg := indexOf(test.args, "-o")
testOutput := []byte{}
if idxOfOutputArg > -1 {
outLocation := test.args[idxOfOutputArg+1]
stat, err := os.Stat(outLocation)
stat, err = os.Stat(outLocation)
assert.NoError(t, err)
// _, err = clearOutputFile(outLocation)
// if err != nil {
// log.Fatal(err)
// }

// If the output arg specified is a directory, concat the contents for comparison.
if stat.IsDir() {
files, err := os.ReadDir(outLocation)
if err != nil {
Expand All @@ -176,6 +194,7 @@ func runCLITest(t *testing.T, test cliTest, goldenFolder string) {
}
}
}

// Since the CLI uses a logger to report errors, the final error message isn't the same as the errors thrown in code.
// Instead, it's wrapped in other os/system errors
// By reading the error text from the logger, we can extract the lower level error that the user would see
Expand Down Expand Up @@ -216,7 +235,7 @@ func getLastSeqNum(archiveURLs []string) uint32 {
}

func clearOutputFile(outputFile string) (string, error) {
f, err := os.OpenFile(outputFile, os.O_RDWR, 0644)
f, err := os.OpenFile(outputFile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 8ea6e38

Please sign in to comment.