Skip to content

Commit

Permalink
- added test for recent file conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
MatrixCrawler committed May 27, 2024
1 parent c7a8efb commit fcbe24a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/recent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func addRecentGeneric(requestedVersion string, installPath string, dist string)
recentFilePath := filepath.Join(installLocation, recentFile)
var recentFileData RecentFiles
if CheckFileExist(recentFilePath) {
unmarshal(recentFilePath, recentFileData)
unmarshal(recentFilePath, &recentFileData)
}
var sliceToCheck []string
if dist == "terraform" {
Expand Down Expand Up @@ -52,7 +52,7 @@ func getRecentVersionsGeneric(installPath string, dist string) ([]string, error)
installLocation := GetInstallLocation(installPath)
recentFilePath := filepath.Join(installLocation, recentFile)
var recentFileData RecentFiles
unmarshal(recentFilePath, recentFileData)
unmarshal(recentFilePath, &recentFileData)
if dist == "terraform" {
return recentFileData.Terraform, nil
} else if dist == "tofu" {
Expand All @@ -61,13 +61,13 @@ func getRecentVersionsGeneric(installPath string, dist string) ([]string, error)
return nil, nil
}

func unmarshal(recentFilePath string, recentFileData RecentFiles) {
func unmarshal(recentFilePath string, recentFileData *RecentFiles) {
recentFileContent, err := os.ReadFile(recentFilePath)
if err != nil {
logger.Errorf("Could not open file %v", recentFilePath)
}
if !strings.HasPrefix(string(recentFileContent), "{") {
convertData(recentFileContent, &recentFileData)
if string(recentFileContent[0:1]) != "{" {
convertData(recentFileContent, recentFileData)
} else {
err = json.Unmarshal(recentFileContent, &recentFileData)
if err != nil {
Expand Down
63 changes: 61 additions & 2 deletions lib/recent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package lib
import (
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)

func Test_convertData(t *testing.T) {
recentFileContent, err := os.ReadFile("../test-data/RECENT")
recentFileContent, err := os.ReadFile("../test-data/recent/RECENT_OLD_FORMAT")
if err != nil {
logger.Error("Could not open file ../test-data/RECENT")
logger.Error("Could not open file ../test-data/recent/RECENT_OLD_FORMAT")
}

var recentFileData RecentFiles
Expand All @@ -20,3 +21,61 @@ func Test_convertData(t *testing.T) {
assert.Equal(t, 3, len(recentFileData.Terraform))
assert.Equal(t, 0, len(recentFileData.Tofu))
}

func Test_saveFile(t *testing.T) {
var recentFileData = RecentFiles{
Terraform: []string{"1.2.3", "4.5.6"},
Tofu: []string{"6.6.6"},
}
temp, err := os.MkdirTemp("", "recent-test")
defer func(path string) {
_ = os.RemoveAll(path)
}(temp)
if err != nil {
t.Errorf("Could not create temporary directory")
}
pathToTempFile := filepath.Join(temp, "recent.json")
saveFile(recentFileData, pathToTempFile)

content, err := os.ReadFile(pathToTempFile)
if err != nil {
t.Errorf("Could not read converted file %v", pathToTempFile)
}
assert.Equal(t, "{\"terraform\":[\"1.2.3\",\"4.5.6\"],\"tofu\":[\"6.6.6\"]}", string(content))
}

func Test_getRecentVersionsGenericForTerraform(t *testing.T) {
logger = InitLogger("DEBUG")
strings, err := getRecentVersionsGeneric("../test-data/recent/recent_as_json/", "terraform")
if err != nil {
t.Error("Unable to get versions from recent file")
}
assert.Equal(t, []string{"1.2.3", "4.5.6"}, strings)
}

func Test_getRecentVersionsGenericForTofu(t *testing.T) {
logger = InitLogger("DEBUG")
strings, err := getRecentVersionsGeneric("../test-data/recent/recent_as_json/", "tofu")
if err != nil {
t.Error("Unable to get versions from recent file")
}
assert.Equal(t, []string{"6.6.6"}, strings)
}

func Test_addRecentGenericForTerraform(t *testing.T) {
logger = InitLogger("DEBUG")
addRecentGeneric("3.7.0", "../test-data/recent/recent_as_json_to_modify/", "terraform")
bytes, err := os.ReadFile("../test-data/recent/recent_as_json_to_modify/.terraform.versions/RECENT")
if err != nil {
t.Error("Could not open file")
t.Error(err)
}
assert.Equal(t, "{\"terraform\":[\"1.2.3\",\"4.5.6\",\"3.7.0\"],\"tofu\":[\"6.6.6\"]}", string(bytes))
addRecentGeneric("1.1.1", "../test-data/recent/recent_as_json_to_modify/", "tofu")
bytes, err = os.ReadFile("../test-data/recent/recent_as_json_to_modify/.terraform.versions/RECENT")
if err != nil {
t.Error("Could not open file")
t.Error(err)
}
assert.Equal(t, "{\"terraform\":[\"1.2.3\",\"4.5.6\",\"3.7.0\"],\"tofu\":[\"6.6.6\",\"1.1.1\"]}", string(bytes))
}
File renamed without changes.
1 change: 1 addition & 0 deletions test-data/recent/recent_as_json/.terraform.versions/RECENT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"terraform":["1.2.3","4.5.6"],"tofu":["6.6.6"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"terraform":["1.2.3","4.5.6","3.7.0"],"tofu":["6.6.6","1.1.1"]}

0 comments on commit fcbe24a

Please sign in to comment.