github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/config/profile/module_test.go (about)

     1  package profile
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/lmorg/murex/shell/autocomplete"
    10  	"github.com/lmorg/murex/test/count"
    11  )
    12  
    13  func testCreateModuleStruct() (posix, plan9, windows Module, err error) {
    14  	var pwd string
    15  
    16  	pwd, err = os.Getwd()
    17  	if err != nil {
    18  		return
    19  	}
    20  
    21  	if runtime.GOOS == "windows" {
    22  		pwd = strings.Replace(pwd, `\`, "/", -1)
    23  	}
    24  
    25  	// this is a terrible kludge!
    26  	source := "../../../../../../../../../../.." + pwd + "/module_test.mx"
    27  
    28  	posix = Module{
    29  		Name:     "test-module-posix",
    30  		Summary:  "A test module",
    31  		Version:  "1.0",
    32  		Source:   source,
    33  		Package:  "/..",
    34  		Disabled: false,
    35  		Dependencies: Dependencies{
    36  			Optional: []string{"foo", "bar"},
    37  			Required: []string{"sh"},
    38  			Platform: []string{"posix"},
    39  		},
    40  	}
    41  
    42  	plan9 = Module{
    43  		Name:     "test-module-plan9",
    44  		Summary:  "A test module",
    45  		Version:  "1.0",
    46  		Source:   source,
    47  		Package:  "/..",
    48  		Disabled: false,
    49  		Dependencies: Dependencies{
    50  			Optional: []string{"foo", "bar"},
    51  			Required: []string{"rc"},
    52  			Platform: []string{"plan9"},
    53  		},
    54  	}
    55  
    56  	windows = Module{
    57  		Name:     "test-module-windows",
    58  		Summary:  "A test module",
    59  		Version:  "1.0",
    60  		Source:   source,
    61  		Package:  "/..",
    62  		Disabled: false,
    63  		Dependencies: Dependencies{
    64  			Optional: []string{"foo", "bar"},
    65  			Required: []string{"cmd.exe"},
    66  			Platform: []string{"windows"},
    67  		},
    68  	}
    69  
    70  	return
    71  }
    72  
    73  func TestIsDisabled(t *testing.T) {
    74  	count.Tests(t, 3)
    75  
    76  	disabled = []string{
    77  		"foo",
    78  		"bar",
    79  	}
    80  
    81  	test := isDisabled("test")
    82  	foo := isDisabled("foo")
    83  	bar := isDisabled("bar")
    84  
    85  	if test {
    86  		t.Errorf("isDisabled true for value not in []string")
    87  	}
    88  
    89  	if !foo || !bar {
    90  		t.Errorf("isDisabled false for value in []string")
    91  	}
    92  }
    93  
    94  func TestValidate(t *testing.T) {
    95  	posix, plan9, windows, err := testCreateModuleStruct()
    96  	if err != nil {
    97  		t.Skipf("Unable to get current working directory: %s", err)
    98  	}
    99  
   100  	if runtime.GOOS == "windows" {
   101  		t.Fatal("This cannot be tested on Windows because drive letter prefixes")
   102  	}
   103  
   104  	_, err = os.Stat(posix.Path())
   105  	if err != nil {
   106  		t.Skip("Unable to stat path. Skipping this test until murex is run for the first time")
   107  	}
   108  
   109  	count.Tests(t, 6)
   110  
   111  	globalExes := map[string]bool{
   112  		"sh":      true,
   113  		"rc":      true,
   114  		"cmd.exe": true,
   115  	}
   116  	autocomplete.GlobalExes.Set(&globalExes)
   117  
   118  	errPosix := posix.validate()
   119  	errPlan9 := plan9.validate()
   120  	errWindows := windows.validate()
   121  
   122  	if runtime.GOOS != "plan9" && runtime.GOOS != "windows" && errPosix != nil {
   123  		t.Errorf("Failed to validate: %s", err)
   124  	}
   125  
   126  	if runtime.GOOS == "plan9" && errPlan9 != nil {
   127  		t.Errorf("Failed to validate: %s", err)
   128  	}
   129  
   130  	if runtime.GOOS == "windows" && errWindows != nil {
   131  		t.Errorf("Failed to validate: %s", err)
   132  	}
   133  
   134  	if (runtime.GOOS == "plan9" || runtime.GOOS == "windows") && errPosix == nil {
   135  		t.Errorf("posix dependency ignored on non-posix OS")
   136  	}
   137  
   138  	if runtime.GOOS != "plan9" && errPlan9 == nil {
   139  		t.Errorf("plan9 dependency ignored on non-plan9 OS")
   140  	}
   141  
   142  	if runtime.GOOS != "windows" && errWindows == nil {
   143  		t.Errorf("windows dependency ignored on non-windows OS")
   144  	}
   145  }