github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/configs/parser_config_dir_test.go (about)

     1  package configs
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  // TestParseLoadConfigDirSuccess is a simple test that just verifies that
    11  // a number of test configuration directories (in testdata/valid-modules)
    12  // can be parsed without raising any diagnostics.
    13  //
    14  // It also re-tests the individual files in testdata/valid-files as if
    15  // they were single-file modules, to ensure that they can be bundled into
    16  // modules correctly.
    17  //
    18  // This test does not verify that reading these modules produces the correct
    19  // module element contents. More detailed assertions may be made on some subset
    20  // of these configuration files in other tests.
    21  func TestParserLoadConfigDirSuccess(t *testing.T) {
    22  	dirs, err := ioutil.ReadDir("testdata/valid-modules")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	for _, info := range dirs {
    28  		name := info.Name()
    29  		t.Run(name, func(t *testing.T) {
    30  			parser := NewParser(nil)
    31  			path := filepath.Join("testdata/valid-modules", name)
    32  
    33  			mod, diags := parser.LoadConfigDir(path)
    34  			if len(diags) != 0 {
    35  				t.Errorf("unexpected diagnostics")
    36  				for _, diag := range diags {
    37  					t.Logf("- %s", diag)
    38  				}
    39  			}
    40  
    41  			if mod.SourceDir != path {
    42  				t.Errorf("wrong SourceDir value %q; want %s", mod.SourceDir, path)
    43  			}
    44  		})
    45  	}
    46  
    47  	// The individual files in testdata/valid-files should also work
    48  	// when loaded as modules.
    49  	files, err := ioutil.ReadDir("testdata/valid-files")
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	for _, info := range files {
    55  		name := info.Name()
    56  		t.Run(fmt.Sprintf("%s as module", name), func(t *testing.T) {
    57  			src, err := ioutil.ReadFile(filepath.Join("testdata/valid-files", name))
    58  			if err != nil {
    59  				t.Fatal(err)
    60  			}
    61  
    62  			parser := testParser(map[string]string{
    63  				"mod/" + name: string(src),
    64  			})
    65  
    66  			_, diags := parser.LoadConfigDir("mod")
    67  			if diags.HasErrors() {
    68  				t.Errorf("unexpected error diagnostics")
    69  				for _, diag := range diags {
    70  					t.Logf("- %s", diag)
    71  				}
    72  			}
    73  		})
    74  	}
    75  
    76  }
    77  
    78  // TestParseLoadConfigDirFailure is a simple test that just verifies that
    79  // a number of test configuration directories (in testdata/invalid-modules)
    80  // produce diagnostics when parsed.
    81  //
    82  // It also re-tests the individual files in testdata/invalid-files as if
    83  // they were single-file modules, to ensure that their errors are still
    84  // detected when loading as part of a module.
    85  //
    86  // This test does not verify that reading these modules produces any
    87  // diagnostics in particular. More detailed assertions may be made on some subset
    88  // of these configuration files in other tests.
    89  func TestParserLoadConfigDirFailure(t *testing.T) {
    90  	dirs, err := ioutil.ReadDir("testdata/invalid-modules")
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	for _, info := range dirs {
    96  		name := info.Name()
    97  		t.Run(name, func(t *testing.T) {
    98  			parser := NewParser(nil)
    99  			path := filepath.Join("testdata/invalid-modules", name)
   100  
   101  			_, diags := parser.LoadConfigDir(path)
   102  			if !diags.HasErrors() {
   103  				t.Errorf("no errors; want at least one")
   104  				for _, diag := range diags {
   105  					t.Logf("- %s", diag)
   106  				}
   107  			}
   108  		})
   109  	}
   110  
   111  	// The individual files in testdata/valid-files should also work
   112  	// when loaded as modules.
   113  	files, err := ioutil.ReadDir("testdata/invalid-files")
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	for _, info := range files {
   119  		name := info.Name()
   120  		t.Run(fmt.Sprintf("%s as module", name), func(t *testing.T) {
   121  			src, err := ioutil.ReadFile(filepath.Join("testdata/invalid-files", name))
   122  			if err != nil {
   123  				t.Fatal(err)
   124  			}
   125  
   126  			parser := testParser(map[string]string{
   127  				"mod/" + name: string(src),
   128  			})
   129  
   130  			_, diags := parser.LoadConfigDir("mod")
   131  			if !diags.HasErrors() {
   132  				t.Errorf("no errors; want at least one")
   133  				for _, diag := range diags {
   134  					t.Logf("- %s", diag)
   135  				}
   136  			}
   137  		})
   138  	}
   139  
   140  }
   141  
   142  func TestIsEmptyDir(t *testing.T) {
   143  	val, err := IsEmptyDir(filepath.Join("testdata", "valid-files"))
   144  	if err != nil {
   145  		t.Fatalf("err: %s", err)
   146  	}
   147  	if val {
   148  		t.Fatal("should not be empty")
   149  	}
   150  }
   151  
   152  func TestIsEmptyDir_noExist(t *testing.T) {
   153  	val, err := IsEmptyDir(filepath.Join("testdata", "nopenopenope"))
   154  	if err != nil {
   155  		t.Fatalf("err: %s", err)
   156  	}
   157  	if !val {
   158  		t.Fatal("should be empty")
   159  	}
   160  }
   161  
   162  func TestIsEmptyDir_noConfigs(t *testing.T) {
   163  	val, err := IsEmptyDir(filepath.Join("testdata", "dir-empty"))
   164  	if err != nil {
   165  		t.Fatalf("err: %s", err)
   166  	}
   167  	if !val {
   168  		t.Fatal("should be empty")
   169  	}
   170  }