github.com/streamingfast/substreams@v1.6.2/cmd/substreams/gui_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func Test_resolveManifestFile(t *testing.T) {
    13  	type args struct {
    14  		input       string
    15  		dirToMake   string
    16  		filesOnDisk []string
    17  	}
    18  	tests := []struct {
    19  		name             string
    20  		args             args
    21  		wantManifestName string
    22  		assertion        require.ErrorAssertionFunc
    23  	}{
    24  		{
    25  			"no input provided",
    26  			args{"", "", []string{"substreams.yaml"}},
    27  			"substreams.yaml",
    28  			require.NoError,
    29  		},
    30  		{
    31  			"no input provided and not substreams.yaml present",
    32  			args{"", "", []string{}},
    33  			"",
    34  			errorEqual("no manifest entered in directory without a manifest"),
    35  		},
    36  		{
    37  			"input provided, valid manifest file",
    38  			args{"substreams-custom.yaml", "", []string{"substreams-custom.yaml"}},
    39  			"substreams-custom.yaml",
    40  			require.NoError,
    41  		},
    42  		{
    43  			"input provided, invalid manifest file",
    44  			args{"substreams-custom.yaml", "", []string{}},
    45  			"",
    46  			errorEqual("read input file info: stat substreams-custom.yaml: no such file or directory"),
    47  		},
    48  		{
    49  			"input provided, valid dir",
    50  			args{"manifests-dir", "manifests-dir", []string{"substreams.yaml"}},
    51  			"manifests-dir/substreams.yaml",
    52  			require.NoError,
    53  		},
    54  		{
    55  			"input provided, invalid dir",
    56  			args{"manifests-dir", "manifests-dir", []string{}},
    57  			"",
    58  			errorEqual("read input file info: stat manifests-dir: no such file or directory"),
    59  		},
    60  		{
    61  			"input provided, valid spkg",
    62  			args{"https://github.com/org/repo/file.spkg", "", []string{}},
    63  			"https://github.com/org/repo/file.spkg",
    64  			require.NoError,
    65  		},
    66  	}
    67  	for _, tt := range tests {
    68  		t.Run(tt.name, func(t *testing.T) {
    69  			root := filepath.Join(t.TempDir(), tt.args.dirToMake)
    70  
    71  			for _, fileOnDisk := range tt.args.filesOnDisk {
    72  				directory := filepath.Join(root, filepath.Dir(fileOnDisk))
    73  				filename := filepath.Join(directory, filepath.Base(fileOnDisk))
    74  
    75  				err := os.MkdirAll(directory, os.ModePerm)
    76  				require.NoError(t, err)
    77  
    78  				err = os.WriteFile(filename, []byte{}, os.ModePerm)
    79  				require.NoError(t, err)
    80  			}
    81  
    82  			cwd, err := os.Getwd()
    83  			require.NoError(t, err)
    84  
    85  			defer func() {
    86  				err := os.Chdir(cwd)
    87  				require.NoError(t, err)
    88  			}()
    89  
    90  			if tt.args.dirToMake != "" {
    91  				root = filepath.Dir(root)
    92  			}
    93  			err = os.Chdir(root)
    94  			require.NoError(t, err)
    95  
    96  			gotManifestName, err := resolveManifestFile(tt.args.input)
    97  			tt.assertion(t, err)
    98  			assert.Equal(t, tt.wantManifestName, gotManifestName)
    99  		})
   100  	}
   101  }
   102  
   103  func errorEqual(expectedErrString string) require.ErrorAssertionFunc {
   104  	return func(t require.TestingT, err error, msgAndArgs ...interface{}) {
   105  		require.EqualError(t, err, expectedErrString, msgAndArgs...)
   106  	}
   107  }