Merge pull request #2945 from somtochiama/reset-test-arg
Reset flag after executing command in tests
This commit is contained in:
@@ -288,36 +288,38 @@ func assertGoldenFile(goldenFile string) assertFunc {
|
|||||||
// is pre-processed with the specified templateValues.
|
// is pre-processed with the specified templateValues.
|
||||||
func assertGoldenTemplateFile(goldenFile string, templateValues map[string]string) assertFunc {
|
func assertGoldenTemplateFile(goldenFile string, templateValues map[string]string) assertFunc {
|
||||||
goldenFileContents, fileErr := os.ReadFile(goldenFile)
|
goldenFileContents, fileErr := os.ReadFile(goldenFile)
|
||||||
return func(output string, err error) error {
|
return assert(
|
||||||
if fileErr != nil {
|
assertSuccess(),
|
||||||
return fmt.Errorf("Error reading golden file '%s': %s", goldenFile, fileErr)
|
func(output string, err error) error {
|
||||||
}
|
if fileErr != nil {
|
||||||
var expectedOutput string
|
return fmt.Errorf("Error reading golden file '%s': %s", goldenFile, fileErr)
|
||||||
if len(templateValues) > 0 {
|
|
||||||
expectedOutput, err = executeTemplate(string(goldenFileContents), templateValues)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Error executing golden template file '%s': %s", goldenFile, err)
|
|
||||||
}
|
}
|
||||||
} else {
|
var expectedOutput string
|
||||||
expectedOutput = string(goldenFileContents)
|
if len(templateValues) > 0 {
|
||||||
}
|
expectedOutput, err = executeTemplate(string(goldenFileContents), templateValues)
|
||||||
if assertErr := assertGoldenValue(expectedOutput)(output, err); assertErr != nil {
|
if err != nil {
|
||||||
// Update the golden files if comparison fails and the update flag is set.
|
return fmt.Errorf("Error executing golden template file '%s': %s", goldenFile, err)
|
||||||
if *update && output != "" {
|
|
||||||
// Skip update if there are template values.
|
|
||||||
if len(templateValues) > 0 {
|
|
||||||
fmt.Println("NOTE: -update flag passed but golden template files can't be updated, please update it manually")
|
|
||||||
} else {
|
|
||||||
if err := os.WriteFile(goldenFile, []byte(output), 0644); err != nil {
|
|
||||||
return fmt.Errorf("failed to update golden file '%s': %v", goldenFile, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
expectedOutput = string(goldenFileContents)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("Mismatch from golden file '%s': %v", goldenFile, assertErr)
|
if assertErr := assertGoldenValue(expectedOutput)(output, err); assertErr != nil {
|
||||||
}
|
// Update the golden files if comparison fails and the update flag is set.
|
||||||
return nil
|
if *update && output != "" {
|
||||||
}
|
// Skip update if there are template values.
|
||||||
|
if len(templateValues) > 0 {
|
||||||
|
fmt.Println("NOTE: -update flag passed but golden template files can't be updated, please update it manually")
|
||||||
|
} else {
|
||||||
|
if err := os.WriteFile(goldenFile, []byte(output), 0644); err != nil {
|
||||||
|
return fmt.Errorf("failed to update golden file '%s': %v", goldenFile, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("Mismatch from golden file '%s': %v", goldenFile, assertErr)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestClusterMode int
|
type TestClusterMode int
|
||||||
@@ -341,7 +343,6 @@ type cmdTestCase struct {
|
|||||||
|
|
||||||
func (cmd *cmdTestCase) runTestCmd(t *testing.T) {
|
func (cmd *cmdTestCase) runTestCmd(t *testing.T) {
|
||||||
actual, testErr := executeCommand(cmd.args)
|
actual, testErr := executeCommand(cmd.args)
|
||||||
|
|
||||||
// If the cmd error is a change, discard it
|
// If the cmd error is a change, discard it
|
||||||
if isChangeError(testErr) {
|
if isChangeError(testErr) {
|
||||||
testErr = nil
|
testErr = nil
|
||||||
@@ -383,15 +384,51 @@ func executeCommand(cmd string) (string, error) {
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resetCmdArgs resets the flags for various cmd
|
||||||
|
// Note: this will also clear default value of the flags set in init()
|
||||||
func resetCmdArgs() {
|
func resetCmdArgs() {
|
||||||
|
*kubeconfigArgs.Namespace = rootArgs.defaults.Namespace
|
||||||
|
alertArgs = alertFlags{}
|
||||||
|
alertProviderArgs = alertProviderFlags{}
|
||||||
|
bootstrapArgs = NewBootstrapFlags()
|
||||||
|
bServerArgs = bServerFlags{}
|
||||||
|
buildKsArgs = buildKsFlags{}
|
||||||
|
checkArgs = checkFlags{}
|
||||||
createArgs = createFlags{}
|
createArgs = createFlags{}
|
||||||
|
deleteArgs = deleteFlags{}
|
||||||
|
diffKsArgs = diffKsFlags{}
|
||||||
|
exportArgs = exportFlags{}
|
||||||
getArgs = GetFlags{}
|
getArgs = GetFlags{}
|
||||||
|
gitArgs = gitFlags{}
|
||||||
|
githubArgs = githubFlags{}
|
||||||
|
gitlabArgs = gitlabFlags{}
|
||||||
|
helmReleaseArgs = helmReleaseFlags{
|
||||||
|
reconcileStrategy: "ChartVersion",
|
||||||
|
}
|
||||||
|
imagePolicyArgs = imagePolicyFlags{}
|
||||||
|
imageRepoArgs = imageRepoFlags{}
|
||||||
|
imageUpdateArgs = imageUpdateFlags{}
|
||||||
|
kustomizationArgs = NewKustomizationFlags()
|
||||||
|
receiverArgs = receiverFlags{}
|
||||||
|
resumeArgs = ResumeFlags{}
|
||||||
|
rhrArgs = reconcileHelmReleaseFlags{}
|
||||||
|
rksArgs = reconcileKsFlags{}
|
||||||
|
secretGitArgs = NewSecretGitFlags()
|
||||||
|
secretHelmArgs = secretHelmFlags{}
|
||||||
|
secretTLSArgs = secretTLSFlags{}
|
||||||
|
sourceBucketArgs = sourceBucketFlags{}
|
||||||
|
sourceGitArgs = newSourceGitFlags()
|
||||||
sourceHelmArgs = sourceHelmFlags{}
|
sourceHelmArgs = sourceHelmFlags{}
|
||||||
sourceOCIRepositoryArgs = sourceOCIRepositoryFlags{}
|
sourceOCIRepositoryArgs = sourceOCIRepositoryFlags{}
|
||||||
sourceGitArgs = sourceGitFlags{}
|
suspendArgs = SuspendFlags{}
|
||||||
sourceBucketArgs = sourceBucketFlags{}
|
tenantArgs = tenantFlags{}
|
||||||
secretGitArgs = NewSecretGitFlags()
|
traceArgs = traceFlags{}
|
||||||
*kubeconfigArgs.Namespace = rootArgs.defaults.Namespace
|
treeKsArgs = TreeKsFlags{}
|
||||||
|
uninstallArgs = uninstallFlags{}
|
||||||
|
versionArgs = versionFlags{
|
||||||
|
output: "yaml",
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isChangeError(err error) bool {
|
func isChangeError(err error) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user