Skip to content

Commit

Permalink
test: Add test for CheckStatus and IsTerraformPluginDirExistent.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Wentzel committed Jun 30, 2021
1 parent ab65a7e commit 695029d
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,37 @@ func setupTestAppInstance(t *testing.T) (app.App, *bytes.Buffer) {
return app, buf
}

func TestIsTerraformPluginDirExistent(t *testing.T) {
t.Run("Should return true if TerraformPluginDir exists", func(t *testing.T) {
app, _ := setupTestAppInstance(t)
createDirIfNotExists(app.Config.TerraformPluginDir)
exists := app.IsTerraformPluginDirExistent()
if !exists {
t.Fatalf("Expected %s to exist", app.Config.TerraformPluginDir)
}
})
t.Run("Should return false if TerraformPluginDir exists", func(t *testing.T) {
app, _ := setupTestAppInstance(t)
exists := app.IsTerraformPluginDirExistent()
if exists {
t.Fatalf("Expected %s to exist", app.Config.TerraformPluginDir)
}
})
}

func TestNew(t *testing.T) {
t.Run("Should create App instance with correct paths", func(t *testing.T) {
app := app.New()
baseDir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
if app.Config.BaseDir != baseDir {
t.Fatalf("expected %v, but got %v", baseDir, app.Config.BaseDir)
}
})
}

func TestActivate(t *testing.T) {
t.Run("Should activate on first use (no directories present)", func(t *testing.T) {
app, buf := setupTestAppInstance(t)
Expand Down Expand Up @@ -96,3 +127,23 @@ func TestDeactivate(t *testing.T) {
}
})
}

func TestCheckStatus(t *testing.T) {
t.Run("Should return active status", func(t *testing.T) {
app, buf := setupTestAppInstance(t)
createDirIfNotExists(app.Config.TerraformPluginDir)
app.CheckStatus()
out := buf.String()
if out != "Status: Active\nLocal providers are used\n" {
t.Fatalf("expected %#v, but got %#v", "Status: Active\nLocal providers are used\n", out)
}
})
t.Run("Should return Deactivated status", func(t *testing.T) {
app, buf := setupTestAppInstance(t)
app.CheckStatus()
out := buf.String()
if out != "Status: Not Active\nAll providers are downloaded from the configured registries\n" {
t.Fatalf("expected %#v, but got %#v", "Status: Not Active\nAll providers are downloaded from the configured registries\n", out)
}
})
}

0 comments on commit 695029d

Please sign in to comment.