Skip to content

Commit

Permalink
Fix failing tests and tidy up code
Browse files Browse the repository at this point in the history
Signed-off-by: Supratik Das <rick.das08@gmail.com>
  • Loading branch information
supra08 committed Mar 26, 2020
1 parent 5f7feac commit 919496f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
13 changes: 9 additions & 4 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,24 @@ func testAcceptance(t *testing.T, when spec.G, it spec.S, packFixturesDir, packP
builderName = value
})

it.After(func() {
h.AssertNil(t, os.RemoveAll(tmpDir))
})

when("builder.toml is invalid", func() {
it("displays an error", func() {
packVer, err := packVersion(packPath)
h.AssertNil(t, err)
packSemver := semver.MustParse(strings.TrimPrefix(strings.Split(packVer, " ")[0], "v"))

h.SkipIf(t, packSemver.GreaterThan(semver.MustParse("0.9.0")) || packSemver.Equal(semver.MustParse("0.0.0")), "unexpected behaviour in current pack version")
h.CopyFile(t, filepath.Join(packFixturesDir, "invalid_builder.toml"), filepath.Join(tmpDir, "invalid_builder.toml"))

_, err := h.RunE(subjectPack("create-builder", "some-builder:build", "--builder-config", filepath.Join(tmpDir, "invalid_builder.toml")))
h.AssertError(t, err, "failed to execute command:")
})
})

it.After(func() {
h.AssertNil(t, os.RemoveAll(tmpDir))
})

when("build", func() {
var repo, repoName string

Expand Down
17 changes: 6 additions & 11 deletions builder/config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ func ReadConfig(path string) (config Config, warnings []string, err error) {
}
defer file.Close()

warnings, obsoleteConfigs, err := getWarningsForObsoleteFields(file)
warnings, err := getWarningsForObsoleteFields(file)
if err != nil {
return Config{}, nil, errors.Wrapf(err, "check warnings for file '%s'", path)
}
if _, err := file.Seek(0, io.SeekStart); err != nil {
return Config{}, nil, errors.Wrap(err, "reset config file pointer")
}

config, err = parseConfig(file, builderDir, path, obsoleteConfigs)
config, err = parseConfig(file, builderDir, path)
if err != nil {
return Config{}, nil, errors.Wrapf(err, "parse contents of '%s'", path)
}
Expand All @@ -96,28 +96,27 @@ func ReadConfig(path string) (config Config, warnings []string, err error) {
return config, warnings, nil
}

func getWarningsForObsoleteFields(reader io.Reader) ([]string, int, error) {
func getWarningsForObsoleteFields(reader io.Reader) ([]string, error) {
var warnings []string

var obsoleteConfig = struct {
Groups []interface{}
}{}

if _, err := toml.DecodeReader(reader, &obsoleteConfig); err != nil {
return nil, 0, err
return nil, err
}

if len(obsoleteConfig.Groups) > 0 {
warnings = append(warnings, fmt.Sprintf("%s field is obsolete in favor of %s", style.Symbol("groups"), style.Symbol("order")))
}

return warnings, len(obsoleteConfig.Groups), nil
return warnings, nil
}

// parseConfig reads a builder configuration from reader and resolves relative buildpack paths using `relativeToDir`
func parseConfig(reader io.Reader, relativeToDir, path string, obsoleteConfigs int) (Config, error) {
func parseConfig(reader io.Reader, relativeToDir, path string) (Config, error) {
builderConfig := Config{}
fmt.Println(obsoleteConfigs)
tomlMetadata, err := toml.DecodeReader(reader, &builderConfig)
if err != nil {
return Config{}, errors.Wrap(err, "decoding toml contents")
Expand All @@ -132,10 +131,6 @@ func parseConfig(reader io.Reader, relativeToDir, path string, obsoleteConfigs i
pluralizedElement += "s"
}

if obsoleteConfigs > 0 {
return builderConfig, nil
}

return Config{}, errors.Errorf("unknown configuration %s %s in %s",
pluralizedElement,
errorKeys,
Expand Down
8 changes: 3 additions & 5 deletions builder/config_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ func testConfig(t *testing.T, when spec.G, it spec.S) {
`), 0666))
})

it("returns warnings", func() {
it("returns error", func() {
_, warns, err := builder.ReadConfig(builderConfigPath)
// h.AssertError(t, err, "parse contents of")
h.AssertNil(t, err)

h.AssertSliceContainsOnly(t, warns, "'groups' field is obsolete in favor of 'order'")
h.AssertError(t, err, "parse contents of")
h.AssertEq(t, len(warns), 0)
})
})

Expand Down

0 comments on commit 919496f

Please sign in to comment.