99 "log"
1010 "os"
1111 "path/filepath"
12+ "regexp"
1213 "strings"
1314 "testing"
1415
@@ -20,6 +21,7 @@ import (
2021
2122 "github.com/hashicorp/terraform/internal/addrs"
2223 "github.com/hashicorp/terraform/internal/command/arguments"
24+ "github.com/hashicorp/terraform/internal/command/views"
2325 "github.com/hashicorp/terraform/internal/configs"
2426 "github.com/hashicorp/terraform/internal/configs/configschema"
2527 "github.com/hashicorp/terraform/internal/depsfile"
@@ -32,6 +34,25 @@ import (
3234 "github.com/hashicorp/terraform/internal/states/statemgr"
3335)
3436
37+ // cleanString removes newlines, and redundant spaces.
38+ func cleanString (s string ) string {
39+ // Replace newlines with a single space.
40+ s = strings .ReplaceAll (s , "\n " , " " )
41+
42+ // Remove other special characters like \r, \t
43+ s = strings .ReplaceAll (s , "\r " , "" )
44+ s = strings .ReplaceAll (s , "\t " , "" )
45+
46+ // Replace multiple spaces with a single space.
47+ spaceRegex := regexp .MustCompile (`\s+` )
48+ s = spaceRegex .ReplaceAllString (s , " " )
49+
50+ // Trim any leading or trailing spaces.
51+ s = strings .TrimSpace (s )
52+
53+ return s
54+ }
55+
3556func TestInit_empty (t * testing.T ) {
3657 // Create a temporary working directory that is empty
3758 td := t .TempDir ()
@@ -52,6 +73,42 @@ func TestInit_empty(t *testing.T) {
5273 if code := c .Run (args ); code != 0 {
5374 t .Fatalf ("bad: \n %s" , done (t ).All ())
5475 }
76+ exp := views .MessageRegistry [views .OutputInitEmptyMessage ].JSONValue
77+ actual := cleanString (done (t ).All ())
78+ if ! strings .Contains (actual , cleanString (exp )) {
79+ t .Fatalf ("expected output to be %q\n , got %q" , exp , actual )
80+ }
81+ }
82+
83+ func TestInit_only_test_files (t * testing.T ) {
84+ // Create a temporary working directory that has only test files and no tf configuration
85+ td := t .TempDir ()
86+ os .MkdirAll (td , 0755 )
87+ defer testChdir (t , td )()
88+
89+ if _ , err := os .Create ("main.tftest.hcl" ); err != nil {
90+ t .Fatalf ("err: %s" , err )
91+ }
92+
93+ ui := new (cli.MockUi )
94+ view , done := testView (t )
95+ c := & InitCommand {
96+ Meta : Meta {
97+ testingOverrides : metaOverridesForProvider (testProvider ()),
98+ Ui : ui ,
99+ View : view ,
100+ },
101+ }
102+
103+ args := []string {}
104+ if code := c .Run (args ); code != 0 {
105+ t .Fatalf ("bad: \n %s" , done (t ).All ())
106+ }
107+ exp := views .MessageRegistry [views .OutputInitSuccessCLIMessage ].JSONValue
108+ actual := cleanString (done (t ).All ())
109+ if ! strings .Contains (actual , cleanString (exp )) {
110+ t .Fatalf ("expected output to be %q\n , got %q" , exp , actual )
111+ }
55112}
56113
57114func TestInit_multipleArgs (t * testing.T ) {
0 commit comments