github.com/opiuman/genqlient@v1.0.0/generate/config_test.go (about)

     1  package generate
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestFindCfg(t *testing.T) {
    12  	cwd, err := os.Getwd()
    13  	require.NoError(t, err)
    14  
    15  	cases := map[string]struct {
    16  		startDir    string
    17  		expectedCfg string
    18  		expectedErr error
    19  	}{
    20  		"yaml in parent directory": {
    21  			startDir:    cwd + "/testdata/find-config/parent/child",
    22  			expectedCfg: cwd + "/testdata/find-config/parent/genqlient.yaml",
    23  		},
    24  		"yaml in current directory": {
    25  			startDir:    cwd + "/testdata/find-config/current",
    26  			expectedCfg: cwd + "/testdata/find-config/current/genqlient.yaml",
    27  		},
    28  		"no yaml": {
    29  			startDir:    cwd + "/testdata/find-config/none/child",
    30  			expectedErr: os.ErrNotExist,
    31  		},
    32  	}
    33  
    34  	for name, tc := range cases {
    35  		t.Run(name, func(t *testing.T) {
    36  			defer func() {
    37  				require.NoError(t, os.Chdir(cwd), "Test cleanup failed")
    38  			}()
    39  
    40  			err = os.Chdir(tc.startDir)
    41  			require.NoError(t, err)
    42  
    43  			path, err := findCfg()
    44  			assert.Equal(t, tc.expectedCfg, path)
    45  			assert.Equal(t, tc.expectedErr, err)
    46  		})
    47  	}
    48  }
    49  
    50  func TestFindCfgInDir(t *testing.T) {
    51  	cwd, err := os.Getwd()
    52  	require.NoError(t, err)
    53  
    54  	cases := map[string]struct {
    55  		startDir string
    56  		found    bool
    57  	}{
    58  		"yaml": {
    59  			startDir: cwd + "/testdata/find-config/filenames/yaml",
    60  			found:    true,
    61  		},
    62  		"yml": {
    63  			startDir: cwd + "/testdata/find-config/filenames/yml",
    64  			found:    true,
    65  		},
    66  		".yaml": {
    67  			startDir: cwd + "/testdata/find-config/filenames/dotyaml",
    68  			found:    true,
    69  		},
    70  		".yml": {
    71  			startDir: cwd + "/testdata/find-config/filenames/dotyml",
    72  			found:    true,
    73  		},
    74  		"none": {
    75  			startDir: cwd + "/testdata/find-config/filenames/none",
    76  			found:    false,
    77  		},
    78  	}
    79  
    80  	for name, tc := range cases {
    81  		t.Run(name, func(t *testing.T) {
    82  			path := findCfgInDir(tc.startDir)
    83  			if tc.found {
    84  				assert.NotEmpty(t, path)
    85  			} else {
    86  				assert.Empty(t, path)
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func TestAbsoluteAndRelativePathsInConfigFiles(t *testing.T) {
    93  	cwd, err := os.Getwd()
    94  	require.NoError(t, err)
    95  
    96  	config, err := ReadAndValidateConfig(
    97  		cwd + "/testdata/find-config/current/genqlient.yaml")
    98  	require.NoError(t, err)
    99  
   100  	require.Equal(t, 1, len(config.Schema))
   101  	require.Equal(
   102  		t,
   103  		cwd+"/testdata/find-config/current/schema.graphql",
   104  		config.Schema[0],
   105  	)
   106  	require.Equal(t, 1, len(config.Operations))
   107  	require.Equal(t, "/tmp/genqlient.graphql", config.Operations[0])
   108  }