github.com/sfdevops1/terrra4orm@v0.11.12-beta1/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 test-fixtures/valid-modules)
    12  // can be parsed without raising any diagnostics.
    13  //
    14  // It also re-tests the individual files in test-fixtures/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("test-fixtures/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("test-fixtures/valid-modules", name)
    32  
    33  			_, 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  	}
    42  
    43  	// The individual files in test-fixtures/valid-files should also work
    44  	// when loaded as modules.
    45  	files, err := ioutil.ReadDir("test-fixtures/valid-files")
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	for _, info := range files {
    51  		name := info.Name()
    52  		t.Run(fmt.Sprintf("%s as module", name), func(t *testing.T) {
    53  			src, err := ioutil.ReadFile(filepath.Join("test-fixtures/valid-files", name))
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  
    58  			parser := testParser(map[string]string{
    59  				"mod/" + name: string(src),
    60  			})
    61  
    62  			_, diags := parser.LoadConfigDir("mod")
    63  			if diags.HasErrors() {
    64  				t.Errorf("unexpected error diagnostics")
    65  				for _, diag := range diags {
    66  					t.Logf("- %s", diag)
    67  				}
    68  			}
    69  		})
    70  	}
    71  
    72  }
    73  
    74  // TestParseLoadConfigDirFailure is a simple test that just verifies that
    75  // a number of test configuration directories (in test-fixtures/invalid-modules)
    76  // produce diagnostics when parsed.
    77  //
    78  // It also re-tests the individual files in test-fixtures/invalid-files as if
    79  // they were single-file modules, to ensure that their errors are still
    80  // detected when loading as part of a module.
    81  //
    82  // This test does not verify that reading these modules produces any
    83  // diagnostics in particular. More detailed assertions may be made on some subset
    84  // of these configuration files in other tests.
    85  func TestParserLoadConfigDirFailure(t *testing.T) {
    86  	dirs, err := ioutil.ReadDir("test-fixtures/invalid-modules")
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	for _, info := range dirs {
    92  		name := info.Name()
    93  		t.Run(name, func(t *testing.T) {
    94  			parser := NewParser(nil)
    95  			path := filepath.Join("test-fixtures/invalid-modules", name)
    96  
    97  			_, diags := parser.LoadConfigDir(path)
    98  			if !diags.HasErrors() {
    99  				t.Errorf("no errors; want at least one")
   100  				for _, diag := range diags {
   101  					t.Logf("- %s", diag)
   102  				}
   103  			}
   104  		})
   105  	}
   106  
   107  	// The individual files in test-fixtures/valid-files should also work
   108  	// when loaded as modules.
   109  	files, err := ioutil.ReadDir("test-fixtures/invalid-files")
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	for _, info := range files {
   115  		name := info.Name()
   116  		t.Run(fmt.Sprintf("%s as module", name), func(t *testing.T) {
   117  			src, err := ioutil.ReadFile(filepath.Join("test-fixtures/invalid-files", name))
   118  			if err != nil {
   119  				t.Fatal(err)
   120  			}
   121  
   122  			parser := testParser(map[string]string{
   123  				"mod/" + name: string(src),
   124  			})
   125  
   126  			_, diags := parser.LoadConfigDir("mod")
   127  			if !diags.HasErrors() {
   128  				t.Errorf("no errors; want at least one")
   129  				for _, diag := range diags {
   130  					t.Logf("- %s", diag)
   131  				}
   132  			}
   133  		})
   134  	}
   135  
   136  }