github.com/secoba/wails/v2@v2.6.4/internal/fs/fs_test.go (about)

     1  package fs
     2  
     3  import (
     4  	"github.com/samber/lo"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/matryer/is"
    10  )
    11  
    12  func TestRelativePath(t *testing.T) {
    13  
    14  	i := is.New(t)
    15  
    16  	cwd, err := os.Getwd()
    17  	i.Equal(err, nil)
    18  
    19  	// Check current directory
    20  	actual := RelativePath(".")
    21  	i.Equal(actual, cwd)
    22  
    23  	// Check 2 parameters
    24  	actual = RelativePath("..", "fs")
    25  	i.Equal(actual, cwd)
    26  
    27  	// Check 3 parameters including filename
    28  	actual = RelativePath("..", "fs", "fs.go")
    29  	expected := filepath.Join(cwd, "fs.go")
    30  	i.Equal(actual, expected)
    31  
    32  }
    33  
    34  func Test_FindFileInParents(t *testing.T) {
    35  	tests := []struct {
    36  		name    string
    37  		setup   func() (startDir string, configDir string)
    38  		wantErr bool
    39  	}{
    40  		{
    41  			name: "should error when no wails.json file is found in local or parent dirs",
    42  			setup: func() (string, string) {
    43  				tempDir := os.TempDir()
    44  				testDir := lo.Must(os.MkdirTemp(tempDir, "projectPath"))
    45  				_ = os.MkdirAll(testDir, 0755)
    46  				return testDir, ""
    47  			},
    48  			wantErr: true,
    49  		},
    50  		{
    51  			name: "should find wails.json in local path",
    52  			setup: func() (string, string) {
    53  				tempDir := os.TempDir()
    54  				testDir := lo.Must(os.MkdirTemp(tempDir, "projectPath"))
    55  				_ = os.MkdirAll(testDir, 0755)
    56  				configFile := filepath.Join(testDir, "wails.json")
    57  				_ = os.WriteFile(configFile, []byte("{}"), 0755)
    58  				return testDir, configFile
    59  			},
    60  			wantErr: false,
    61  		},
    62  		{
    63  			name: "should find wails.json in parent path",
    64  			setup: func() (string, string) {
    65  				tempDir := os.TempDir()
    66  				testDir := lo.Must(os.MkdirTemp(tempDir, "projectPath"))
    67  				_ = os.MkdirAll(testDir, 0755)
    68  				parentDir := filepath.Dir(testDir)
    69  				configFile := filepath.Join(parentDir, "wails.json")
    70  				_ = os.WriteFile(configFile, []byte("{}"), 0755)
    71  				return testDir, configFile
    72  			},
    73  			wantErr: false,
    74  		},
    75  	}
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			path, expectedPath := tt.setup()
    79  			defer func() {
    80  				if expectedPath != "" {
    81  					_ = os.Remove(expectedPath)
    82  				}
    83  			}()
    84  			got := FindFileInParents(path, "wails.json")
    85  			if got != expectedPath {
    86  				t.Errorf("FindFileInParents() got = %v, want %v", got, expectedPath)
    87  			}
    88  		})
    89  	}
    90  }