Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursively search upwards for a changie config #652

Merged
merged 4 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/Added-20240510-012051.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Added
body: Search upwards for a changie config file
time: 2024-05-10T01:20:51.00611009-07:00
custom:
Issue: "651"
2 changes: 1 addition & 1 deletion .github/workflows/build-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.21
go-version: 1.22

- name: Check out code
uses: actions/checkout@v4
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.21
go-version: 1.22

- name: Check out code
uses: actions/checkout@v4
Expand All @@ -40,7 +40,7 @@ jobs:
with:
# Required: the version of golangci-lint is required and must be specified
# without patch version: we always use the latest patch version.
version: v1.57
version: v1.58

- name: Gen
run: go run main.go gen
Expand All @@ -49,9 +49,9 @@ jobs:
run: go test -coverprofile=c.out ./...

- name: Coverage
uses: codecov/codecov-action@v4
uses: codacy/codacy-coverage-reporter-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: miniscruff/changie
file: ./c.out
fail_ci_if_error: true
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
coverage-reports: ./c.out
force-coverage-parser: go

8 changes: 8 additions & 0 deletions core/change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func TestWriteChange(t *testing.T) {
then.Nil(t, err)
}

func TestLoadChangeFailsIfNoFile(t *testing.T) {
then.WithTempDir(t)

_, err := LoadChange("missing_file.yaml")

then.NotNil(t, err)
}

func TestLoadChangeFromPath(t *testing.T) {
then.WithTempDir(t)
then.Nil(t, os.WriteFile("some_file.yaml", []byte("kind: A\nbody: hey\n"), CreateFileMode))
Expand Down
46 changes: 40 additions & 6 deletions core/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package core

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"

"github.com/Masterminds/semver/v3"
"gopkg.in/yaml.v3"
Expand All @@ -28,6 +31,8 @@
".changie.yml",
}

var ErrConfigNotFound = errors.New("no changie config found")

// GetVersions will return, in semver sorted order, all released versions
type GetVersions func(Config) ([]*semver.Version, error)

Expand Down Expand Up @@ -446,6 +451,40 @@
return false, nil
}

// findConfigUpwards will recursively look up until we find a changie config file
func findConfigUpwards() ([]byte, error) {
currDir, err := os.Getwd()
if err != nil {
return nil, err
}

for {
for _, path := range ConfigPaths {
bs, err := os.ReadFile(filepath.Join(currDir, path))

Check warning on line 463 in core/config.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

core/config.go#L463

The application dynamically constructs file or path information.
if err == nil || !errors.Is(err, fs.ErrNotExist) {
return bs, nil
}
}

err := os.Chdir("..")
if err != nil {
return nil, err
}

lastDir := currDir

currDir, err = os.Getwd()
if err != nil {
return nil, err
}

// Keep going up, until going up is unchanged.
if lastDir == currDir {
return nil, ErrConfigNotFound
}
}
}

// LoadConfig will load the config from the default path
func LoadConfig() (*Config, error) {
var (
Expand All @@ -458,12 +497,7 @@
if customPath != "" {
bs, err = os.ReadFile(customPath)
} else {
for _, path := range ConfigPaths {
bs, err = os.ReadFile(path)
if err == nil {
break
}
}
bs, err = findConfigUpwards()
}

if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions core/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,41 @@ func TestLoadConfigFromEnvVar(t *testing.T) {
then.Equals(t, "header.rst", config.HeaderPath)
}

func TestLoadConfigFromEnvVarMissingFile(t *testing.T) {
then.WithTempDir(t)
t.Setenv("CHANGIE_CONFIG_PATH", filepath.Join("custom", "missing.yaml"))

_, err := LoadConfig()
then.NotNil(t, err)
}

func TestLoadConfigMultipleLayersWithoutAConfig(t *testing.T) {
then.WithTempDir(t)

then.Nil(t, os.MkdirAll(filepath.Join("a", "b", "c"), CreateDirMode))
then.Nil(t, os.Chdir("a"))
then.Nil(t, os.Chdir("b"))
then.Nil(t, os.Chdir("c"))

_, err := LoadConfig()
then.Err(t, ErrConfigNotFound, err)
}

func TestLoadConfigMultipleLayersDown(t *testing.T) {
then.WithTempDir(t)
then.WriteFile(t, []byte("changesDir: C\nheaderPath: header.rst\n"), ".changie.yml")

then.Nil(t, os.MkdirAll(filepath.Join("a", "b", "c"), CreateDirMode))
then.Nil(t, os.Chdir("a"))
then.Nil(t, os.Chdir("b"))
then.Nil(t, os.Chdir("c"))

config, err := LoadConfig()
then.Nil(t, err)
then.Equals(t, "C", config.ChangesDir)
then.Equals(t, "header.rst", config.HeaderPath)
}

func TestDefaultFragmentTemplateWithProjects(t *testing.T) {
then.WithTempDir(t)

Expand Down
Loading
Loading