github.com/AlpineAIO/wails/v2@v2.0.0-beta.32.0.20240505041856-1047a8fa5fef/internal/project/project_test.go (about)

     1  package project_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/AlpineAIO/wails/v2/internal/project"
    10  	"github.com/samber/lo"
    11  )
    12  
    13  func TestProject_GetFrontendDir(t *testing.T) {
    14  	cwd := lo.Must(os.Getwd())
    15  	tests := []struct {
    16  		name      string
    17  		inputJSON string
    18  		want      string
    19  		wantError bool
    20  	}{
    21  		{
    22  			name:      "Should use 'frontend' by default",
    23  			inputJSON: "{}",
    24  			want:      filepath.ToSlash(filepath.Join(cwd, "frontend")),
    25  			wantError: false,
    26  		},
    27  		{
    28  			name:      "Should resolve a relative path with no project path",
    29  			inputJSON: `{"frontend:dir": "./frontend"}`,
    30  			want:      filepath.ToSlash(filepath.Join(cwd, "frontend")),
    31  			wantError: false,
    32  		},
    33  		{
    34  			name: "Should resolve a relative path with project path set",
    35  			inputJSON: func() string {
    36  				if runtime.GOOS == "windows" {
    37  					return `{"frontend:dir": "./frontend", "projectdir": "C:\\project"}`
    38  				} else {
    39  					return `{"frontend:dir": "./frontend", "projectdir": "/home/user/project"}`
    40  				}
    41  			}(),
    42  			want: func() string {
    43  				if runtime.GOOS == "windows" {
    44  					return `C:/project/frontend`
    45  				} else {
    46  					return `/home/user/project/frontend`
    47  				}
    48  			}(),
    49  			wantError: false,
    50  		},
    51  		{
    52  			name: "Should honour an absolute path",
    53  			inputJSON: func() string {
    54  				if runtime.GOOS == "windows" {
    55  					return `{"frontend:dir": "C:\\frontend", "projectdir": "C:\\project"}`
    56  				} else {
    57  					return `{"frontend:dir": "/home/myproject/frontend", "projectdir": "/home/user/project"}`
    58  				}
    59  			}(),
    60  			want: func() string {
    61  				if runtime.GOOS == "windows" {
    62  					return `C:/frontend`
    63  				} else {
    64  					return `/home/myproject/frontend`
    65  				}
    66  			}(),
    67  			wantError: false,
    68  		},
    69  	}
    70  	for _, tt := range tests {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			proj, err := project.Parse([]byte(tt.inputJSON))
    73  			if err != nil && !tt.wantError {
    74  				t.Errorf("Error parsing project: %s", err)
    75  			}
    76  			got := proj.GetFrontendDir()
    77  			got = filepath.ToSlash(got)
    78  			if got != tt.want {
    79  				t.Errorf("GetFrontendDir() = %v, want %v", got, tt.want)
    80  			}
    81  		})
    82  	}
    83  }
    84  func TestProject_GetBuildDir(t *testing.T) {
    85  	cwd := lo.Must(os.Getwd())
    86  	tests := []struct {
    87  		name      string
    88  		inputJSON string
    89  		want      string
    90  		wantError bool
    91  	}{
    92  		{
    93  			name:      "Should use 'build' by default",
    94  			inputJSON: "{}",
    95  			want:      filepath.ToSlash(filepath.Join(cwd, "build")),
    96  			wantError: false,
    97  		},
    98  		{
    99  			name:      "Should resolve a relative path with no project path",
   100  			inputJSON: `{"build:dir": "./build"}`,
   101  			want:      filepath.ToSlash(filepath.Join(cwd, "build")),
   102  			wantError: false,
   103  		},
   104  		{
   105  			name:      "Should resolve a relative path with project path set",
   106  			inputJSON: `{"build:dir": "./build", "projectdir": "/home/user/project"}`,
   107  			want:      "/home/user/project/build",
   108  			wantError: false,
   109  		},
   110  		{
   111  			name: "Should honour an absolute path",
   112  			inputJSON: func() string {
   113  				if runtime.GOOS == "windows" {
   114  					return `{"build:dir": "C:\\build", "projectdir": "C:\\project"}`
   115  				} else {
   116  					return `{"build:dir": "/home/myproject/build", "projectdir": "/home/user/project"}`
   117  				}
   118  			}(),
   119  			want: func() string {
   120  				if runtime.GOOS == "windows" {
   121  					return `C:/build`
   122  				} else {
   123  					return `/home/myproject/build`
   124  				}
   125  			}(),
   126  			wantError: false,
   127  		},
   128  	}
   129  	for _, tt := range tests {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			proj, err := project.Parse([]byte(tt.inputJSON))
   132  			if err != nil && !tt.wantError {
   133  				t.Errorf("Error parsing project: %s", err)
   134  			}
   135  			got := proj.GetBuildDir()
   136  			got = filepath.ToSlash(got)
   137  			if got != tt.want {
   138  				t.Errorf("GetFrontendDir() = %v, want %v", got, tt.want)
   139  			}
   140  		})
   141  	}
   142  }