mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2024-12-22 16:26:25 +00:00
chore(lock): test setting lockfile path from config
This commit is contained in:
parent
0fdf9c77ae
commit
c9a1474297
@ -1,62 +1,88 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/cupcakearmy/autorestic/internal/flags"
|
"github.com/cupcakearmy/autorestic/internal/flags"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testDirectory = "autorestic_test_tmp"
|
|
||||||
|
|
||||||
// All tests must share the same lock file as it is only initialized once
|
// All tests must share the same lock file as it is only initialized once
|
||||||
func setup(t *testing.T) {
|
func setup(t *testing.T) {
|
||||||
d, err := os.MkdirTemp("", testDirectory)
|
t.Helper()
|
||||||
if err != nil {
|
cleanup := func() {
|
||||||
log.Fatalf("error creating temp dir: %v", err)
|
flags.LOCKFILE_PATH = ""
|
||||||
return
|
config = nil
|
||||||
}
|
once = sync.Once{}
|
||||||
// set config file location
|
|
||||||
viper.SetConfigFile(d + "/.autorestic.yml")
|
|
||||||
|
|
||||||
t.Cleanup(func() {
|
|
||||||
os.RemoveAll(d)
|
|
||||||
viper.Reset()
|
viper.Reset()
|
||||||
})
|
}
|
||||||
|
|
||||||
|
cleanup()
|
||||||
|
d := t.TempDir()
|
||||||
|
viper.SetConfigFile(d + "/.autorestic.yml")
|
||||||
|
viper.Set("version", 2)
|
||||||
|
viper.WriteConfig()
|
||||||
|
|
||||||
|
t.Cleanup(cleanup)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetLockfilePath(t *testing.T) {
|
func TestGetLockfilePath(t *testing.T) {
|
||||||
t.Run("when flags.LOCKFILE_PATH is set", func(t *testing.T) {
|
t.Run("user specified", func(t *testing.T) {
|
||||||
flags.LOCKFILE_PATH = "/path/to/my/autorestic.lock.yml"
|
testCases := []struct {
|
||||||
defer func() { flags.LOCKFILE_PATH = "" }()
|
name string
|
||||||
|
flag string
|
||||||
|
config string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "flag and config",
|
||||||
|
flag: "/flag.lock.yml",
|
||||||
|
config: "/config.lock.yml",
|
||||||
|
expected: "/flag.lock.yml",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "flag only",
|
||||||
|
flag: "/flag.lock.yml",
|
||||||
|
config: "",
|
||||||
|
expected: "/flag.lock.yml",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "config only",
|
||||||
|
flag: "",
|
||||||
|
config: "/config.lock.yml",
|
||||||
|
expected: "/config.lock.yml",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
setup(t)
|
||||||
|
flags.LOCKFILE_PATH = testCase.flag
|
||||||
|
if testCase.config != "" {
|
||||||
|
viper.Set("lockfile", testCase.config)
|
||||||
|
err := viper.WriteConfig()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
p := getLockfilePath()
|
p := getLockfilePath()
|
||||||
|
assert.Equal(t, testCase.expected, p)
|
||||||
if p != "/path/to/my/autorestic.lock.yml" {
|
})
|
||||||
t.Errorf("got %v, want %v", p, "/path/to/my/autorestic.lock.yml")
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("when flags.LOCKFILE_PATH is set", func(t *testing.T) {
|
t.Run("default", func(t *testing.T) {
|
||||||
d, err := os.MkdirTemp("", testDirectory)
|
setup(t)
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("error creating temp dir: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
viper.SetConfigFile(d + "/.autorestic.yml")
|
|
||||||
defer viper.Reset()
|
|
||||||
|
|
||||||
flags.LOCKFILE_PATH = ""
|
configPath := viper.ConfigFileUsed()
|
||||||
|
expectedLockfile := path.Join(path.Dir(configPath), ".autorestic.lock.yml")
|
||||||
|
|
||||||
p := getLockfilePath()
|
p := getLockfilePath()
|
||||||
|
assert.Equal(t, expectedLockfile, p)
|
||||||
if p != d+"/.autorestic.lock.yml" {
|
|
||||||
t.Errorf("got %v, want %v", p, d+"/.autorestic.lock.yml")
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user