Return filename from publisher
This commit is contained in:
parent
dc19bd4e3b
commit
ade4a8efdc
@ -9,7 +9,7 @@ import (
|
||||
func TestNewMode(t *testing.T) {
|
||||
mode, err := statediff.NewMode("csv")
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
if mode != statediff.CSV {
|
||||
|
@ -31,12 +31,12 @@ func createCSVFilePath(path string) string {
|
||||
return filePath
|
||||
}
|
||||
|
||||
func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) error {
|
||||
func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) (string, error) {
|
||||
filePath := createCSVFilePath(p.Config.Path)
|
||||
|
||||
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
@ -58,11 +58,11 @@ func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) error {
|
||||
for _, value := range data {
|
||||
err := writer.Write(value)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
func accumulateUpdatedAccountRows(sd builder.StateDiff) [][]string {
|
||||
|
@ -41,8 +41,8 @@ func NewPublisher(config statediff.Config) (*publisher, error) {
|
||||
func (p *publisher) PublishStateDiff(sd *builder.StateDiff) (string, error) {
|
||||
switch p.Config.Mode {
|
||||
case statediff.CSV:
|
||||
return "", p.publishStateDiffToCSV(*sd)
|
||||
return p.publishStateDiffToCSV(*sd)
|
||||
default:
|
||||
return "", p.publishStateDiffToCSV(*sd)
|
||||
return p.publishStateDiffToCSV(*sd)
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -77,6 +78,7 @@ func TestPublisher(t *testing.T) {
|
||||
type Test func(t *testing.T)
|
||||
|
||||
var tests = []Test{
|
||||
testFileName,
|
||||
testColumnHeaders,
|
||||
testAccountDiffs,
|
||||
testWhenNoDiff,
|
||||
@ -108,23 +110,34 @@ func removeFilesFromDir(dir string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func testFileName(t *testing.T) {
|
||||
fileName, err := publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(fileName, dir) {
|
||||
t.Errorf(testhelpers.TestFailureFormatString, t.Name(), dir, fileName)
|
||||
}
|
||||
}
|
||||
|
||||
func testColumnHeaders(t *testing.T) {
|
||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
file, err := getTestDiffFile(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
lines, err := csv.NewReader(file).ReadAll()
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
if len(lines) < 1 {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[0], p.Headers) {
|
||||
t.Error()
|
||||
@ -135,29 +148,29 @@ func testAccountDiffs(t *testing.T) {
|
||||
// it persists the created, updated and deleted account diffs to a CSV file
|
||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
file, err := getTestDiffFile(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
lines, err := csv.NewReader(file).ReadAll()
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
if len(lines) <= 3 {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[1], expectedCreatedAccountRow) {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[2], expectedUpdatedAccountRow) {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[3], expectedDeletedAccountRow) {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -166,21 +179,21 @@ func testWhenNoDiff(t *testing.T) {
|
||||
emptyDiff := builder.StateDiff{}
|
||||
_, err = publisher.PublishStateDiff(&emptyDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
file, err := getTestDiffFile(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
lines, err := csv.NewReader(file).ReadAll()
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
if !equals(len(lines), 1) {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -189,28 +202,28 @@ func testDefaultPublisher(t *testing.T) {
|
||||
config := statediff.Config{Path: dir}
|
||||
publisher, err = p.NewPublisher(config)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
file, err := getTestDiffFile(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
lines, err := csv.NewReader(file).ReadAll()
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(len(lines), 4) {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[0], p.Headers) {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -219,33 +232,33 @@ func testDefaultDirectory(t *testing.T) {
|
||||
config := statediff.Config{}
|
||||
publisher, err = p.NewPublisher(config)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
err := os.Chdir(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
file, err := getTestDiffFile(dir)
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
|
||||
lines, err := csv.NewReader(file).ReadAll()
|
||||
if err != nil {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(len(lines), 4) {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
if !equals(lines[0], p.Headers) {
|
||||
t.Errorf(testhelpers.TestFailedFormatString, t.Name(), err)
|
||||
t.Errorf(testhelpers.ErrorFormatString, t.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
package testhelpers
|
||||
|
||||
var TestFailedFormatString = "Test failed: %s, %+v"
|
||||
var ErrorFormatString = "Error: %s, %+v"
|
||||
var TestFailureFormatString = "Test failed: %s\nexpected %+v, got %+v\n"
|
||||
|
Loading…
Reference in New Issue
Block a user