Write manifest and lock in TOML

This commit is contained in:
Carolyn Van Slyck 2017-03-20 13:14:02 -05:00
Родитель d3135b8a1e
Коммит 6366f9ed68
6 изменённых файлов: 22 добавлений и 9 удалений

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

@ -29,7 +29,7 @@ func TestDeriveManifestAndLock(t *testing.T) {
t.Fatal(err)
}
got, err := m.(*Manifest).MarshalJSON()
got, err := m.(*Manifest).MarshalTOML()
if err != nil {
t.Fatal(err)
}

14
fs.go
Просмотреть файл

@ -5,7 +5,6 @@
package dep
import (
"encoding/json"
"io"
"io/ioutil"
"os"
@ -16,6 +15,13 @@ import (
"github.com/pkg/errors"
)
// Marshaler is the interface implemented by types that
// can marshal themselves into valid TOML.
// TODO(carolynvs) Consider adding this to go-toml?
type Marshaler interface {
MarshalTOML() (string, error)
}
func IsRegular(name string) (bool, error) {
// TODO: lstat?
fi, err := os.Stat(name)
@ -59,19 +65,19 @@ func IsNonEmptyDir(name string) (bool, error) {
return len(files) != 0, nil
}
func writeFile(path string, in json.Marshaler) error {
func writeFile(path string, in Marshaler) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
b, err := in.MarshalJSON()
s, err := in.MarshalTOML()
if err != nil {
return err
}
_, err = f.Write(b)
_, err = f.WriteString(s)
return err
}

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

@ -79,7 +79,7 @@ func TestWriteManifest(t *testing.T) {
Ignores: []string{"github.com/foo/bar"},
}
got, err := m.MarshalJSON()
got, err := m.MarshalTOML()
if err != nil {
t.Fatalf("Error while marshaling valid manifest to TOML: %q", err)
}

7
testdata/txn_writer/expected_lock.toml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,7 @@
memo = "595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c"
[[projects]]
name = "github.com/sdboyer/dep-test"
packages = ["."]
revision = "2a3a211e171803acb82d1d5d42ceb53228f51751"
version = "1.0.0"

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

@ -365,7 +365,7 @@ fail:
func (sw *SafeWriter) PrintPreparedActions() error {
if sw.Payload.HasManifest() {
fmt.Println("Would have written the following manifest.json:")
m, err := sw.Payload.Manifest.MarshalJSON()
m, err := sw.Payload.Manifest.MarshalTOML()
if err != nil {
return errors.Wrap(err, "ensure DryRun cannot serialize manifest")
}
@ -375,7 +375,7 @@ func (sw *SafeWriter) PrintPreparedActions() error {
if sw.Payload.HasLock() {
if sw.Payload.LockDiff == nil {
fmt.Println("Would have written the following lock.json:")
l, err := sw.Payload.Lock.MarshalJSON()
l, err := sw.Payload.Lock.MarshalTOML()
if err != nil {
return errors.Wrap(err, "ensure DryRun cannot serialize lock")
}

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

@ -15,7 +15,7 @@ import (
const safeWriterProject = "safewritertest"
const safeWriterGoldenManifest = "txn_writer/expected_manifest.json"
const safeWriterGoldenLock = "txn_writer/expected_lock.json"
const safeWriterGoldenLock = "txn_writer/expected_lock.toml"
func TestSafeWriter_BadInput_MissingRoot(t *testing.T) {
h := test.NewHelper(t)