Skip to content
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
35 changes: 33 additions & 2 deletions .github/workflows/govulncheck.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ on:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
go:
- { go-version: stable }
- { go-version-file: go.mod }
deps:
- locked
- latest
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
with:
persist-credentials: false
- uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16
with:
go-version: ${{ matrix.go.go-version }}
go-version-file: ${{ matrix.go.go-version-file }}
- uses: geomys/sandboxed-step@7d75eb49d17fdeeb3656b3a57d35932d205bcfb9
with:
run: |
export GODEBUG=cpu.aes=off
if [ "${{ matrix.deps }}" == "latest" ]; then
go get -u ./...
go mod tidy
fi
go test -v ./...
govulncheck:
runs-on: ubuntu-latest
steps:
Expand All @@ -17,5 +45,8 @@ jobs:
- uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16
with:
go-version-file: go.mod
- run: |
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
- uses: geomys/sandboxed-step@7d75eb49d17fdeeb3656b3a57d35932d205bcfb9
with:
run: |
export GODEBUG=cpu.aes=off
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
13 changes: 8 additions & 5 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ jobs:
- uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16
with:
go-version-file: go.mod
- run: |
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
- uses: geomys/sandboxed-step@7d75eb49d17fdeeb3656b3a57d35932d205bcfb9
with:
run: |
export GODEBUG=cpu.aes=off
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
lint:
runs-on: ubuntu-latest
needs: govulncheck
Expand All @@ -30,7 +33,7 @@ jobs:
persist-credentials: false
- uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16
with:
go-version: 1.26
go-version-file: go.mod
- uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a # v9.3.0
test:
runs-on: ubuntu-latest
Expand All @@ -41,7 +44,7 @@ jobs:
persist-credentials: false
- uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16
with:
go-version: 1.26
go-version-file: go.mod
- run: go test -v ./...
release:
runs-on: ubuntu-latest
Expand All @@ -54,7 +57,7 @@ jobs:
persist-credentials: false
- uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16
with:
go-version: 1.26
go-version-file: go.mod
- uses: go-semantic-release/action@2e9dc4247a6004f8377781bef4cb9dad273a741f # v1.24.1
if: github.ref == 'refs/heads/main'
with:
Expand Down
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var (
CommitSHA string = "unknown"
// Config contains all configurations
Config *models.Config

// userHomeDir is the function used to retrieve the user's home directory.
// It can be overridden in tests.
userHomeDir = os.UserHomeDir
)

// getConfigFile returns the config.json file.
Expand All @@ -36,7 +40,7 @@ func getConfigFile(filename string) (*os.File, error) {
}
f, err := os.Open("config/" + filename) // #nosec G304
if err != nil {
homeDir, homeErr := os.UserHomeDir()
homeDir, homeErr := userHomeDir()
if homeErr != nil {
return nil, homeErr
}
Expand Down
80 changes: 28 additions & 52 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,17 @@ import (
"testing"
)

func createTempConfigFile(t *testing.T, content string) (string, func()) {
dir := t.TempDir()
file := filepath.Join(dir, "test_config.json")
err := os.WriteFile(file, []byte(content), 0o644)
if err != nil {
t.Fatalf("Failed to write temp config file: %v", err)
}
return file, func() {
err := os.Remove(file)
if err != nil {
t.Logf("Could not remove file %v", file)
}
}
}

func TestNewConfig_LocalFile(t *testing.T) {
configContent := `{"logLevel":"debug"}`
file, cleanup := createTempConfigFile(t, configContent)
defer cleanup()

// Move file to expected local path
if err := os.MkdirAll("config", 0o755); err != nil {
t.Log("could not create directory 'config'")
t.Fatalf("could not create directory 'config': %v", err)
}
if err := os.Rename(file, "config/test_config.json"); err != nil {
t.Logf("could not rename file %v", file)
configPath := filepath.Join("config", "test_config.json")
if err := os.WriteFile(configPath, []byte(configContent), 0o644); err != nil {
t.Fatalf("could not write config file: %v", err)
}
defer func() {
if err := os.Remove("config/test_config.json"); err != nil {
t.Log("could not remove config file")
}
}()
defer os.Remove(configPath) // nolint

cfg, err := newConfig("test_config.json")
if err != nil {
Expand All @@ -50,23 +29,20 @@ func TestNewConfig_LocalFile(t *testing.T) {

func TestNewConfig_HomeFallback(t *testing.T) {
configContent := `{"logLevel":"info"}`
file, cleanup := createTempConfigFile(t, configContent)
defer cleanup()

homeDir, _ := os.UserHomeDir()
configDir := filepath.Join(homeDir, ".config", "nirimgr")
tmpHome := t.TempDir()
oldUserHomeDir := userHomeDir
userHomeDir = func() (string, error) { return tmpHome, nil }
defer func() { userHomeDir = oldUserHomeDir }()

configDir := filepath.Join(tmpHome, ".config", "nirimgr")
if err := os.MkdirAll(configDir, 0o755); err != nil {
t.Logf("could not create directory '%v'", configDir)
t.Fatalf("could not create config dir: %v", err)
}
configPath := filepath.Join(configDir, "test_config.json")
if err := os.Rename(file, configPath); err != nil {
t.Logf("could not rename file '%v' to '%v'", file, configPath)
if err := os.WriteFile(configPath, []byte(configContent), 0o644); err != nil {
t.Fatalf("could not write config file: %v", err)
}
defer func() {
if err := os.Remove(configPath); err != nil {
t.Log("could not remove config path")
}
}()

cfg, err := newConfig("test_config.json")
if err != nil {
Expand All @@ -79,19 +55,15 @@ func TestNewConfig_HomeFallback(t *testing.T) {

func TestConfigure_SetsGlobalConfig(t *testing.T) {
configContent := `{"logLevel":"warn"}`
file, cleanup := createTempConfigFile(t, configContent)
defer cleanup()

if err := os.MkdirAll("config", 0o755); err != nil {
t.Log("could not create directory 'config'")
t.Fatalf("could not create directory 'config': %v", err)
}
if err := os.Rename(file, "config/test_config.json"); err != nil {
t.Logf("could not rename file '%v'", file)
configPath := filepath.Join("config", "test_config.json")
if err := os.WriteFile(configPath, []byte(configContent), 0o644); err != nil {
t.Fatalf("could not write config file: %v", err)
}
defer func() {
if err := os.Remove("config/test_config.json"); err != nil {
t.Log("could not remove test config file")
}
}()
defer os.Remove(configPath) // nolint

err := Configure("test_config.json")
if err != nil {
Expand All @@ -103,10 +75,14 @@ func TestConfigure_SetsGlobalConfig(t *testing.T) {
}

func TestGetConfigFile_Error(t *testing.T) {
err := os.Remove("config/test_config.json")
if err != nil {
t.Log("could not remove test config.")
}
tmpHome := t.TempDir()
oldUserHomeDir := userHomeDir
userHomeDir = func() (string, error) { return tmpHome, nil }
defer func() { userHomeDir = oldUserHomeDir }()

// Ensure no local config file exists
os.Remove(filepath.Join("config", "test_config.json")) // nolint

cfg, err := getConfigFile("test_config.json")
if err == nil || cfg != nil {
t.Error("Expected error when config files are missing")
Expand Down