Skip to content

Commit

Permalink
config: add Convert test
Browse files Browse the repository at this point in the history
note: confpath convert was reordered to happen first to mitigate a very unlikely edge case where lab is run in `.config` causing the converted toml file to land in `.config` instead of `.config/lab/`
  • Loading branch information
zaquestion committed Sep 5, 2020
1 parent 51d9b7d commit 6d8163f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
- linux

go:
- 1.13.x
- 1.15.x

# use containers which run faster and have cache
sudo: false
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Head to the [releases](https://github.com/zaquestion/lab/releases) page and down
### Source

Required
* [Go 1.13+](https://golang.org/doc/install)
* [Go 1.15+](https://golang.org/doc/install)

```
git clone git@github.com:zaquestion/lab
Expand Down
11 changes: 4 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,11 @@ func ConvertHCLtoTOML(oldpath string, newpath string, file string) {
oldconfig := oldpath + "/" + file + ".hcl"
newconfig := newpath + "/" + file + ".toml"

_, err := os.Stat(oldconfig)
if os.IsNotExist(err) {
if _, err := os.Stat(oldconfig); os.IsNotExist(err) {
return
}

_, err = os.Stat(newconfig)
if err == nil {
if _, err := os.Stat(newconfig); err == nil {
return
}

Expand All @@ -120,8 +118,7 @@ func ConvertHCLtoTOML(oldpath string, newpath string, file string) {
viper.WriteConfigAs(newconfig)

// delete the old config HCL file
err = os.Remove(oldconfig)
if err != nil {
if err := os.Remove(oldconfig); err != nil {
fmt.Println("Warning: Could not delete old config file", oldconfig)
}

Expand Down Expand Up @@ -193,8 +190,8 @@ func LoadConfig() (string, string, string, string, bool) {

// Convert old hcl files to toml format.
// NO NEW FILES SHOULD BE ADDED BELOW.
ConvertHCLtoTOML(".", ".", "lab")
ConvertHCLtoTOML(confpath, labconfpath, "lab")
ConvertHCLtoTOML(".", ".", "lab")
var labgitDir string
gitDir, err := git.GitDir()
if err == nil {
Expand Down
52 changes: 42 additions & 10 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"

Expand All @@ -16,10 +17,7 @@ import (
)

func TestNewConfig(t *testing.T) {
testconf, err := ioutil.TempDir("", "testconf-")
if err != nil {
t.Fatal(err)
}
testconf := t.TempDir()

t.Run("create config", func(t *testing.T) {
old := os.Stdout // keep backup of the real stdout
Expand Down Expand Up @@ -73,15 +71,11 @@ func TestNewConfig(t *testing.T) {
token = "abcde12345"
`, string(cfgData))
})
os.RemoveAll(testconf)
viper.Reset()
}

func TestNewConfigHostOverride(t *testing.T) {
testconf, err := ioutil.TempDir("", "testconf-")
if err != nil {
t.Fatal(err)
}
testconf := t.TempDir()

os.Setenv("LAB_CORE_HOST", "https://gitlab2.zaquestion.io")

Expand Down Expand Up @@ -141,6 +135,44 @@ func TestNewConfigHostOverride(t *testing.T) {
token = "abcde12345"
`, string(cfgData))
})
os.RemoveAll(testconf)
viper.Reset()
}

func TestConvertHCLtoTOML(t *testing.T) {
tmpDir := t.TempDir()
oldCnfPath := filepath.Join(tmpDir, "lab.hcl")
newCnfPath := filepath.Join(tmpDir, "lab.toml")
oldCnf, err := os.Create(oldCnfPath)
if err != nil {
t.Fatal(err)
}
oldCnf.WriteString(`"core" = {
"host" = "https://gitlab.com"
"token" = "foobar"
"user" = "lab-testing"
}`)

ConvertHCLtoTOML(tmpDir, tmpDir, "lab")

_, err = os.Stat(oldCnfPath)
assert.True(t, os.IsNotExist(err))

_, err = os.Stat(newCnfPath)
assert.NoError(t, err)

newCnf, err := os.Open(newCnfPath)
if err != nil {
t.Fatal(err)
}
cfgData, err := ioutil.ReadAll(newCnf)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, `
[core]
host = "https://gitlab.com"
token = "foobar"
user = "lab-testing"
`, string(cfgData))
}

0 comments on commit 6d8163f

Please sign in to comment.