-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] Add more tests for coverage (#46)
- Loading branch information
Showing
10 changed files
with
300 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/cacoco/codemetagenerator/internal/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func refresh(writer utils.Writer, basedir string, httpClient *http.Client) error { | ||
// update licenses file | ||
err := downloadSPDXLicenses(basedir, httpClient, true) | ||
if err != nil { | ||
handleErr(writer, err) | ||
return writer.Errorf("unable to update SPDX licenses file: %s", err.Error()) | ||
} | ||
writer.Println("✅ Successfully updated SPDX licenses file.") | ||
return nil | ||
} | ||
|
||
// refreshCmd represents the refresh command | ||
var refreshCmd = &cobra.Command{ | ||
Use: "refresh", | ||
Short: "Refresh the list of current SPDX IDs (https://spdx.org/licenses/)", | ||
Long: ` | ||
Use this command to refresh the stored list of currently supported SPDX IDs from https://spdx.org/licenses/. | ||
See: https://spdx.dev/learn/handling-license-info/#why`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return refresh(&utils.StdoutWriter{}, utils.UserHomeDir, utils.MkHttpClient()) | ||
}, | ||
} | ||
|
||
func init() { | ||
licensesCmd.AddCommand(refreshCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/cacoco/codemetagenerator/internal/utils" | ||
"github.com/ohler55/ojg/oj" | ||
"github.com/onsi/gomega" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Test_ExecuteRefreshCmd(t *testing.T) { | ||
g := gomega.NewWithT(t) | ||
|
||
temp := t.TempDir() | ||
// setup | ||
os.Mkdir(utils.GetHomeDir(temp), 0755) | ||
file, err := os.ReadFile("../testdata/spdx-full-licenses.json") | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
var stack utils.Stack[string] | ||
stack.Push(string(file)) | ||
httpClient := utils.NewTestHttpClient(&stack) | ||
writer := utils.TestWriter{} | ||
|
||
refreshCmd := &cobra.Command{Use: "refresh", RunE: func(cmd *cobra.Command, args []string) error { | ||
return refresh(writer, temp, httpClient) | ||
}, | ||
} | ||
buf := bytes.NewBufferString("") | ||
refreshCmd.SetOut(buf) | ||
refreshCmd.SetErr(buf) | ||
refreshCmd.SetArgs([]string{}) | ||
|
||
err = refreshCmd.Execute() | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
// read converted test file | ||
var expected map[string]string = make(map[string]string) | ||
file, err = os.ReadFile("../testdata/spdx-licenses.json") | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
oj.Unmarshal(file, &expected) | ||
|
||
var actual map[string]string = make(map[string]string) | ||
fileBytes, err := utils.LoadFile(utils.GetLicensesFilePath(temp)) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
oj.Unmarshal(fileBytes, &actual) | ||
|
||
// check "downloaded" file against converted test file | ||
g.Ω(actual).Should(gomega.Equal(expected)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.