-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
165 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package file | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestFindOutTargets(t *testing.T) { | ||
tag := "test" | ||
data := []byte(` | ||
test 1 | ||
test 2 | ||
test 3 | ||
test 4 | ||
t 4 | ||
te 5 | ||
tes 6 | ||
test 7 | ||
tes 8 | ||
te 9 | ||
t 10 | ||
testing 11 | ||
testin 12 | ||
testi 13 | ||
`) | ||
result, err := FindOutTargets(tag, string(data)) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
expected := []Target{ | ||
{Tag: "test", Library: "1"}, | ||
{Tag: "test", Library: "2"}, | ||
{Tag: "test", Library: "7"}, | ||
} | ||
if fmt.Sprintf("%v", result) != fmt.Sprintf("%v", expected) { | ||
t.Errorf("unexpected result, result: %v, expected: %v", result, expected) | ||
} | ||
} | ||
|
||
type tmpLibrary struct { | ||
Path string | ||
Value string | ||
} | ||
|
||
func TestReplaceTags(t *testing.T) { | ||
lib := tmpLibrary{ | ||
Path: fmt.Sprintf("%v/lib/1", Suite.Temp), | ||
Value: "hello 1", | ||
} | ||
|
||
Suite.CreateTempFile(lib.Path, lib.Value, t) | ||
defer Suite.RemoveTemp(t) | ||
|
||
in := []Target{{Tag: "test", Library: lib.Path}} | ||
data := ` | ||
test {{.Path}} | ||
hello test | ||
` | ||
result, err := ReplaceTargets(in, Suite.BuildPayloadFile(data, lib, t)) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
expectedData := ` | ||
{{.Value}} | ||
hello test | ||
` | ||
expected := Suite.BuildPayloadFile(expectedData, lib, t) | ||
if result != expected { | ||
t.Errorf("unexpected result, result: %v, expected: %v", result, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package file | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/alecthomas/template" | ||
) | ||
|
||
// Suite is a instance of Test struct | ||
var Suite = &Test{Temp: "./tmp"} | ||
|
||
// Test is a struct to help make tests | ||
type Test struct { | ||
Temp string | ||
} | ||
|
||
// BuildPayloadFile is a func to help interpolate value in payload struct of file | ||
func (tst *Test) BuildPayloadFile(data string, str interface{}, t *testing.T) string { | ||
bufferData := bytes.NewBuffer(nil) | ||
tmpl := template.Must(template.New("data").Parse(data)) | ||
if err := tmpl.Execute(bufferData, str); err != nil { | ||
t.Fatal(err) | ||
} | ||
return bufferData.String() | ||
} | ||
|
||
// CreateTempFile is a func to create a file inside temp context | ||
func (tst *Test) CreateTempFile(path, s string, t *testing.T) { | ||
if _, err := Build(path, s); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
// RemoveTemp is a func to remove temp folder | ||
func (tst *Test) RemoveTemp(t *testing.T) { | ||
if err := os.RemoveAll(tst.Temp); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |