From e1fb4f505eaa87e6719b12f02fb8480dae643b1b Mon Sep 17 00:00:00 2001 From: Enrique Lacal Date: Tue, 17 Sep 2024 16:13:49 +0100 Subject: [PATCH] add test for exit ffconfig main Signed-off-by: Enrique Lacal --- ffconfig/main_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ffconfig/main_test.go b/ffconfig/main_test.go index 31c4c4ba2..f7b136561 100644 --- a/ffconfig/main_test.go +++ b/ffconfig/main_test.go @@ -68,6 +68,24 @@ func TestConfigMigrateCmd(t *testing.T) { assert.NoError(t, err) } +func TestMain(t *testing.T) { + // Run the exiting code when FLAG is set + if os.Getenv("FLAG") == "0" { + rootCmd.SetArgs([]string{"migrate", "-f", configPath}) + main() + return + } + + // Run the test in a subprocess + cmd := exec.Command(os.Args[0], "-test.run=TestMain") + cmd.Env = append(os.Environ(), "FLAG=0") + err := cmd.Run() + + // Cast the error as *exec.ExitError and compare the result + _, ok := err.(*exec.ExitError) + assert.Equal(t, false, ok) +} + func TestConfigMigrateCmdWriteOutput(t *testing.T) { tmpDir, err := os.MkdirTemp(os.TempDir(), "out") assert.NoError(t, err)