Skip to content

Commit

Permalink
Fix test for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
dude333 committed Mar 22, 2021
1 parent 19930f2 commit f13d3cb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
3 changes: 2 additions & 1 deletion parsers/sectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

func TestFromSector(t *testing.T) {
const filename = "/tmp/test_sectors.yml"
tempDir, _ := ioutil.TempDir("", "rapina-test")
filename := tempDir + "/test_sectors.yml"

createYaml(filename)
s, _, _ := FromSector("GRENDENE S.A.", filename)
Expand Down
11 changes: 6 additions & 5 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rapina
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"

Expand Down Expand Up @@ -170,7 +171,7 @@ func promptUser(list []string) (result string) {

//
// filename cleans up the filename and returns the path/filename
func filename(path, name string) (filepath string, err error) {
func filename(path, name string) (fpath string, err error) {
clean := func(r rune) rune {
switch r {
case ' ', ',', '/', '\\':
Expand All @@ -181,20 +182,20 @@ func filename(path, name string) (filepath string, err error) {
path = strings.TrimSuffix(path, "/")
name = strings.TrimSuffix(name, ".")
name = strings.Map(clean, name)
filepath = path + "/" + name + ".xlsx"
fpath = filepath.FromSlash(path + "/" + name + ".xlsx")

const max = 50
var x int
for x = 1; x <= max; x++ {
_, err = os.Stat(filepath)
_, err = os.Stat(fpath)
if err == nil {
// File exists, try again with another name
filepath = fmt.Sprintf("%s/%s(%d).xlsx", path, name, x)
fpath = fmt.Sprintf("%s/%s(%d).xlsx", path, name, x)
} else if os.IsNotExist(err) {
err = nil // reset error
break
} else {
err = fmt.Errorf("file %s stat error: %v", filepath, err)
err = fmt.Errorf("file %s stat error: %v", fpath, err)
return
}
}
Expand Down
24 changes: 13 additions & 11 deletions report_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package rapina

import "testing"
import (
"io/ioutil"
"path/filepath"
"testing"
)

func TestFilename(t *testing.T) {
tempDir, _ := ioutil.TempDir("", "rapina-test")

table := []struct {
path string
name string
expected string
}{
{"/tmp/test", "sample", "/tmp/test/sample.xlsx"},
{"/tmp", "File 100", "/tmp/File_100.xlsx"},
{"/tmp", "An,odd/file\\name", "/tmp/An_odd_file_name.xlsx"},
{tempDir + "/test", "sample", tempDir + "/test/sample.xlsx"},
{tempDir, "File 100", tempDir + "/File_100.xlsx"},
{tempDir, "An,odd/file\\name", tempDir + "/An_odd_file_name.xlsx"},
}

for _, x := range table {
returned, err := filename(x.path, x.name)
expected := filepath.FromSlash(x.expected)
if err != nil {
t.Errorf("filename returned an error %v.", err)
} else if returned != x.expected {
t.Errorf("filename got: %s, want: %s.", returned, x.expected)
} else if returned != expected {
t.Errorf("filename got: %s, want: %s.", returned, expected)
}
}

_, err := filename("/tmp2", "test")
if err == nil {
t.Errorf("filename should have returned an error.")
}

}

0 comments on commit f13d3cb

Please sign in to comment.