44package command
55
66import (
7+ "bytes"
78 "encoding/json"
89 "io/ioutil"
910 "os"
@@ -21,6 +22,7 @@ import (
2122 "github.com/hashicorp/terraform/internal/providers"
2223 testing_provider "github.com/hashicorp/terraform/internal/providers/testing"
2324 "github.com/hashicorp/terraform/internal/states"
25+ "github.com/hashicorp/terraform/internal/states/statefile"
2426 "github.com/hashicorp/terraform/internal/states/statemgr"
2527 "github.com/hashicorp/terraform/version"
2628)
@@ -1105,6 +1107,87 @@ func TestShow_corruptStatefile(t *testing.T) {
11051107 }
11061108}
11071109
1110+ // TestShow_stateStore tests the `show` command with no arguments, which uses the state that
1111+ // matches the selected workspace. In this test the default workspace is in use.
1112+ func TestShow_stateStore (t * testing.T ) {
1113+ originalState := testState ()
1114+ originalState .SetOutputValue (
1115+ addrs.OutputValue {Name : "test" }.Absolute (addrs .RootModuleInstance ),
1116+ cty .ObjectVal (map [string ]cty.Value {
1117+ "attr" : cty .NullVal (cty .DynamicPseudoType ),
1118+ "null" : cty .NullVal (cty .String ),
1119+ "list" : cty .ListVal ([]cty.Value {cty .NullVal (cty .Number )}),
1120+ }),
1121+ false ,
1122+ )
1123+
1124+ // Create a temporary working directory that is empty
1125+ td := t .TempDir ()
1126+ testCopyDir (t , testFixturePath ("state-store-unchanged" ), td )
1127+ t .Chdir (td )
1128+
1129+ // Get bytes describing the state
1130+ var stateBuf bytes.Buffer
1131+ if err := statefile .Write (statefile .New (originalState , "" , 1 ), & stateBuf ); err != nil {
1132+ t .Fatalf ("error during test setup: %s" , err )
1133+ }
1134+
1135+ // Create a mock that contains a persisted "default" state that uses the bytes from above.
1136+ mockProvider := mockPluggableStateStorageProvider (t )
1137+ mockProvider .MockStates = map [string ]interface {}{
1138+ "default" : stateBuf .Bytes (),
1139+ }
1140+ mockProviderAddress := addrs .NewDefaultProvider ("test" )
1141+ providerSource , close := newMockProviderSource (t , map [string ][]string {
1142+ "hashicorp/test" : {"1.0.0" },
1143+ })
1144+ defer close ()
1145+
1146+ view , done := testView (t )
1147+ c := & ShowCommand {
1148+ Meta : Meta {
1149+ AllowExperimentalFeatures : true ,
1150+ testingOverrides : & testingOverrides {
1151+ Providers : map [addrs.Provider ]providers.Factory {
1152+ mockProviderAddress : providers .FactoryFixed (mockProvider ),
1153+ },
1154+ },
1155+ ProviderSource : providerSource ,
1156+ View : view ,
1157+ },
1158+ }
1159+
1160+ args := []string {
1161+ "-no-color" ,
1162+ }
1163+ code := c .Run (args )
1164+ output := done (t )
1165+ if code != 0 {
1166+ t .Fatalf ("unexpected exit status %d; want 0\n got: %s" , code , output .Stderr ())
1167+ }
1168+
1169+ expected := `# test_instance.foo:
1170+ resource "test_instance" "foo" {
1171+ id = "bar"
1172+ }
1173+
1174+
1175+ Outputs:
1176+
1177+ test = {
1178+ list = [
1179+ null,
1180+ ]
1181+ }`
1182+ actual := strings .TrimSpace (output .Stdout ())
1183+ if actual != expected {
1184+ t .Fatalf ("expected = %s'\n got = %s" ,
1185+ expected ,
1186+ actual ,
1187+ )
1188+ }
1189+ }
1190+
11081191// showFixtureSchema returns a schema suitable for processing the configuration
11091192// in testdata/show. This schema should be assigned to a mock provider
11101193// named "test".
0 commit comments