* Expanded diff test case to verify that multiple projects
  are formatted properly
This commit is contained in:
Carolyn Van Slyck 2017-03-22 17:11:43 -05:00
Родитель 28c2316fba
Коммит 23facf8bbf
5 изменённых файлов: 113 добавлений и 75 удалений

63
testdata/txn_writer/expected_diff_output.txt поставляемый
Просмотреть файл

@ -1,34 +1,31 @@
Memo: 595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c -> 2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e
Add: [
{
"name": "github.com/stuff/realthing",
"version": "2.0.0",
"revision": "1f02e52d6bac308da54ab84a234c58a98ca82347",
"packages": [
"."
]
}
]
Remove: [
{
"name": "github.com/stuff/placeholder",
"version": "2.0.0",
"revision": "6694017eeb4e20fd277b049bf29dba4895c97234",
"packages": [
"."
]
}
]
Modify: [
{
"name": "github.com/foo/bar",
"source": "+ http://github.example.com/foo/bar",
"version": "+ 1.2.0",
"branch": "- master",
"revision": "f24338400f072ef18125ae0fbe6b06fe6d1783e7 -> 2a3a211e171803acb82d1d5d42ceb53228f51751",
"packages": [
"- placeholder",
"+ thing"
]
}
]
Add:
[[projects]]
name = "github.com/sdboyer/deptest"
packages = ["."]
revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf"
version = "v1.0.0"
[[projects]]
name = "github.com/stuff/realthing"
packages = ["."]
revision = "1f02e52d6bac308da54ab84a234c58a98ca82347"
version = "2.0.0"
Remove:
[[projects]]
name = "github.com/stuff/placeholder"
packages = ["."]
revision = "6694017eeb4e20fd277b049bf29dba4895c97234"
version = "2.0.0"
Modify:
[[projects]]
branch = "- master"
name = "github.com/foo/bar"
packages = ["- placeholder","+ thing"]
revision = "f24338400f072ef18125ae0fbe6b06fe6d1783e7 -> 2a3a211e171803acb82d1d5d42ceb53228f51751"
source = "+ http://github.example.com/foo/bar"
version = "+ 1.2.0"

2
testdata/txn_writer/original_lock.toml поставляемый
Просмотреть файл

@ -6,7 +6,7 @@ memo = "595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c"
revision = "f24338400f072ef18125ae0fbe6b06fe6d1783e7"
packages = ["placeholder", "util"]
[[packages]]
[[projects]]
name = "github.com/stuff/placeholder"
version = "2.0.0"
revision = "6694017eeb4e20fd277b049bf29dba4895c97234"

8
testdata/txn_writer/updated_lock.toml поставляемый
Просмотреть файл

@ -7,8 +7,14 @@ memo = "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e"
revision = "2a3a211e171803acb82d1d5d42ceb53228f51751"
packages = ["thing","util"]
[[packages]]
[[projects]]
name = "github.com/stuff/realthing"
version = "2.0.0"
revision = "1f02e52d6bac308da54ab84a234c58a98ca82347"
packages = ["."]
[[projects]]
name = "github.com/sdboyer/deptest"
packages = ["."]
revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf"
version = "v1.0.0"

Просмотреть файл

@ -7,7 +7,6 @@ package dep
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
@ -15,6 +14,7 @@ import (
"sort"
"strings"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
"github.com/sdboyer/gps"
)
@ -67,40 +67,40 @@ func (diff *LockDiff) Format() (string, error) {
var buf bytes.Buffer
if diff.HashDiff != nil {
buf.WriteString(fmt.Sprintf("Memo: %s\n", diff.HashDiff))
buf.WriteString(fmt.Sprintf("Memo: %s\n\n", diff.HashDiff))
}
writeDiffs := func(diffs []LockedProjectDiff) error {
for i := 0; i < len(diffs); i++ {
chunk, err := diffs[i].MarshalTOML()
if err != nil {
return err
}
buf.WriteString(chunk)
}
buf.WriteString("\n")
return nil
}
if len(diff.Add) > 0 {
buf.WriteString("Add: ")
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
err := enc.Encode(diff.Add)
buf.WriteString("Add:")
err := writeDiffs(diff.Add)
if err != nil {
return "", errors.Wrap(err, "Unable to format LockDiff.Add")
}
}
if len(diff.Remove) > 0 {
buf.WriteString("Remove: ")
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
err := enc.Encode(diff.Remove)
buf.WriteString("Remove:")
err := writeDiffs(diff.Remove)
if err != nil {
return "", errors.Wrap(err, "Unable to format LockDiff.Remove")
}
}
if len(diff.Modify) > 0 {
buf.WriteString("Modify: ")
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
err := enc.Encode(diff.Modify)
buf.WriteString("Modify:")
err := writeDiffs(diff.Modify)
if err != nil {
return "", errors.Wrap(err, "Unable to format LockDiff.Modify")
}
@ -113,12 +113,52 @@ func (diff *LockDiff) Format() (string, error) {
// Fields are only populated when there is a difference, otherwise they are empty.
// TODO(carolynvs) this should be moved to gps
type LockedProjectDiff struct {
Name gps.ProjectRoot `json:"name"`
Source *StringDiff `json:"source,omitempty"`
Version *StringDiff `json:"version,omitempty"`
Branch *StringDiff `json:"branch,omitempty"`
Revision *StringDiff `json:"revision,omitempty"`
Packages []StringDiff `json:"packages,omitempty"`
Name gps.ProjectRoot
Source *StringDiff
Version *StringDiff
Branch *StringDiff
Revision *StringDiff
Packages []StringDiff
}
func (diff *LockedProjectDiff) MarshalTOML() (string, error) {
prj := make(map[string]interface{})
prj["name"] = string(diff.Name)
if diff.Source != nil {
prj["source"] = diff.Source.String()
}
if diff.Version != nil {
prj["version"] = diff.Version.String()
}
if diff.Branch != nil {
prj["branch"] = diff.Branch.String()
}
if diff.Revision != nil {
prj["revision"] = diff.Revision.String()
}
if len(diff.Packages) > 0 {
p := make([]interface{}, len(diff.Packages))
for i := 0; i < len(diff.Packages); i++ {
p[i] = diff.Packages[i].String()
}
prj["packages"] = p
}
m := make(map[string]interface{})
m["projects"] = []map[string]interface{}{prj}
t, err := toml.TreeFromMap(m)
if err != nil {
return "", errors.Wrap(err, "Unable to marshal lock diff to TOML tree")
}
result, err := t.ToTomlString()
return result, errors.Wrap(err, "Unable to marshal lock diff to TOML string")
}
type StringDiff struct {
@ -142,15 +182,6 @@ func (diff StringDiff) String() string {
return diff.Current
}
func (diff StringDiff) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
err := enc.Encode(diff.String())
return buf.Bytes(), err
}
// VendorBehavior defines when the vendor directory should be written.
type VendorBehavior int

Просмотреть файл

@ -518,12 +518,16 @@ func TestSafeWriter_DiffLocks(t *testing.T) {
t.Fatalf("Expected the lock diff to contain the updated hash: expected %s, got %s", pc.Project.Lock.Memo, updatedLock.Memo)
}
if len(diff.Add) != 1 {
t.Fatalf("Expected the lock diff to contain 1 added project, got %d", len(diff.Add))
if len(diff.Add) != 2 {
t.Fatalf("Expected the lock diff to contain 2 added projects, got %d", len(diff.Add))
} else {
add := diff.Add[0]
if add.Name != "github.com/stuff/realthing" {
t.Errorf("expected new project github.com/stuff/realthing, got %s", add.Name)
add1 := diff.Add[0]
if add1.Name != "github.com/sdboyer/deptest" {
t.Errorf("expected new project[0] github.com/sdboyer/deptest, got %s", add1.Name)
}
add2 := diff.Add[1]
if add2.Name != "github.com/stuff/realthing" {
t.Errorf("expected new project[1] github.com/stuff/realthing, got %s", add2.Name)
}
}