Skip to content

Commit

Permalink
Break the cyclic dependency with mypkg/toolbox
Browse files Browse the repository at this point in the history
  • Loading branch information
wunderbarb committed Sep 22, 2020
1 parent a1a2080 commit 619681d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/wunderbarb/test
go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.6.1
github.com/udhos/equalfile v0.3.0
github.com/wunderbarb/mypkg v0.6.1
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
46 changes: 41 additions & 5 deletions random.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// V0.9.1a
// V0.9.2
// Author: Diehl E.
// (C) Sony Pictures Entertainment, Sep 2020

Expand All @@ -8,10 +8,9 @@ import (
"encoding/csv"
"math/rand"
"os"
"path"
"path/filepath"
"time"

"github.com/wunderbarb/mypkg/toolbox"
)

// Rng is a randomly seeded random number generator that can be used for tests.
Expand Down Expand Up @@ -143,7 +142,7 @@ func RandomAlphaString(size int, t AlphaNumType) string {
// of `columns` x `rows` using as separator `sep` with
// random size fields.
func RandomCSVFile(name string, columns int, rows int, sep rune) error {
name = toolbox.SetExtension(name, "csv")
name = setExtension(name, "csv")
f, err := os.Create(name)
if err != nil {
return err
Expand Down Expand Up @@ -200,7 +199,7 @@ func RandomFile(size int, ext string, inTestdata bool) (string, error) {
//
// It returns the name of the generated file (without) the path.
func RandomFileWithDir(size int, ext string, path string) (string, error) {
name := toolbox.SetExtension(RandomID(), ext)
name := setExtension(RandomID(), ext)
if path != "" {
name = filepath.Join(path, name)
}
Expand All @@ -218,3 +217,40 @@ func RandomFileWithDir(size int, ext string, path string) (string, error) {
}
return filepath.Base(f.Name()), nil
}

// setExtension ensures that the extension ext is present at
// the end of the file. If ext does not have a trailing '.', it adds
// the proper extension.
//
// Is the same than mypkg/setExtension but need to break cyclic mod.
func setExtension(name string, ext string) string {
if ext == "" {
return name
}
if ext[:1] != "." {
ext = "." + ext
}
return strip(name, ext) + ext
}

// strip removes the extension if present.
// @1- ext is the extension to test. If there is no trailing
// '.', it is added.
// returns the file name without the extension if present.
//
// Is the same than mypkg/strip but need to break cyclic mod.
func strip(name string, ext string) string {
if ext == "" {
return name
}
if ext[:1] != "." {
ext = "." + ext
}
if path.Ext(name) == ext {

s := name
l := len(name) - len(ext)
name = s[:l]
}
return name
}

0 comments on commit 619681d

Please sign in to comment.