Skip to content

Commit

Permalink
Update the fastly.toml manifest if missing manifest_version. (#220)
Browse files Browse the repository at this point in the history
* Write the fastly.toml manifest after Read if manifest_version missing.

* Prevent test from overwriting fixture data.
  • Loading branch information
Integralist authored Mar 17, 2021
1 parent 2cc715c commit 1b41384
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ lint:
.PHONY: test
test:
go test -race $(TESTARGS)

.PHONY: build
build:
go build ./cmd/fastly
Expand Down
25 changes: 12 additions & 13 deletions pkg/compute/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ func (d *Data) Authors() ([]string, Source) {
//
// In the near future release the `Version` field will be removed and so there
// will be less ambiguity around what this type refers to.
type Version struct {
int
}
type Version int

// UnmarshalText manages multiple scenarios where historically the manifest
// version was a string value and not an integer.
Expand All @@ -133,20 +131,19 @@ type Version struct {
// but instead will return an error because it exceeds the current
// ManifestLatestVersion version of 1.
func (v *Version) UnmarshalText(text []byte) error {
var err error

s := string(text)

if v.int, err = strconv.Atoi(s); err == nil {
if i, err := strconv.Atoi(s); err == nil {
*v = Version(i)
return nil
}

if f, err := strconv.ParseFloat(s, 32); err == nil {
intfl := int(f)
if intfl == 0 {
v.int = 1
*v = 1
} else {
v.int = intfl
*v = Version(intfl)
}
return nil
}
Expand All @@ -157,14 +154,15 @@ func (v *Version) UnmarshalText(text []byte) error {
// A length of 3 presumes a semver (e.g. 0.1.0)
if len(segs) == 3 {
if segs[0] != "0" {
if v.int, err = strconv.Atoi(segs[0]); err == nil {
if v.int > ManifestLatestVersion {
if i, err := strconv.Atoi(segs[0]); err == nil {
if i > ManifestLatestVersion {
return errors.ErrUnrecognisedManifestVersion
}
*v = Version(i)
return nil
}
} else {
v.int = 1
*v = 1
return nil
}
}
Expand Down Expand Up @@ -217,8 +215,8 @@ func (f *File) Read(fpath string) error {

f.exists = true

if f.ManifestVersion.int == 0 {
f.ManifestVersion.int = 1
if f.ManifestVersion == 0 {
f.ManifestVersion = 1

// TODO: Provide link to v1 schema once published publicly.
//
Expand All @@ -228,6 +226,7 @@ func (f *File) Read(fpath string) error {
once.Do(func() {
text.Warning(f.output, "The fastly.toml was missing a `manifest_version` field. A default schema version of `1` will be used.")
text.Break(f.output)
f.Write(fpath)
})
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/compute/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manifest

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -41,6 +42,29 @@ func TestManifest(t *testing.T) {
},
}

// NOTE: the fixture file "fastly-invalid-missing-version.toml" will be
// overwritten by the test as the internal logic is supposed to add back into
// the manifest a manifest_version field if one isn't found.
//
// To ensure future test runs complete successfully we do an initial read of
// the data and then write it back out when the tests have completed.
path, err := filepath.Abs(filepath.Join(prefix, "fastly-invalid-missing-version.toml"))
if err != nil {
t.Fatal(err)
}

b, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}

defer func() {
err = ioutil.WriteFile(path, b, 0644)
if err != nil {
t.Fatal(err)
}
}()

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var m File
Expand Down

0 comments on commit 1b41384

Please sign in to comment.