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

     1  package profile_test
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	_ "github.com/lmorg/murex/builtins"
     9  	"github.com/lmorg/murex/config/profile"
    10  	"github.com/lmorg/murex/lang"
    11  	"github.com/lmorg/murex/test/count"
    12  	"github.com/lmorg/murex/utils/consts"
    13  	"github.com/lmorg/murex/utils/home"
    14  )
    15  
    16  func TestProfilePaths(t *testing.T) {
    17  	count.Tests(t, 10)
    18  
    19  	var path string
    20  	home := home.MyDir
    21  	temp := t.TempDir()
    22  
    23  	// get running settings
    24  
    25  	bakPreload := os.Getenv(profile.PreloadEnvVar)
    26  	bakModule := os.Getenv(profile.ModuleEnvVar)
    27  	bakProfile := os.Getenv(profile.ProfileEnvVar)
    28  
    29  	defer func() {
    30  		if err := os.Setenv(profile.PreloadEnvVar, bakPreload); err != nil {
    31  			t.Errorf("Unable to restore env var settings: '%s' to '%s'", profile.PreloadEnvVar, bakPreload)
    32  		}
    33  
    34  		if err := os.Setenv(profile.ModuleEnvVar, bakModule); err != nil {
    35  			t.Errorf("Unable to restore env var settings: '%s' to '%s'", profile.ModuleEnvVar, bakModule)
    36  		}
    37  
    38  		if err := os.Setenv(profile.ProfileEnvVar, bakProfile); err != nil {
    39  			t.Errorf("Unable to restore env var settings: '%s' to '%s'", profile.ProfileEnvVar, bakProfile)
    40  		}
    41  	}()
    42  
    43  	// unset env vars (default paths)
    44  
    45  	os.Unsetenv(profile.PreloadEnvVar) // don't care about errors
    46  	path = profile.PreloadPath()
    47  	if !strings.HasPrefix(path, home) {
    48  		t.Error("Unexpected PreloadPath():")
    49  		t.Logf("Expected prefix:  '%s'", home)
    50  		t.Logf("Actual full path: '%s'", path)
    51  	}
    52  
    53  	os.Unsetenv(profile.ModuleEnvVar) // don't care about errors
    54  	path = profile.ModulePath()
    55  	if !strings.HasPrefix(path, home) {
    56  		t.Error("Unexpected ModulePath():")
    57  		t.Logf("Expected prefix:  '%s'", home)
    58  		t.Logf("Actual full path: '%s'", path)
    59  	}
    60  
    61  	os.Unsetenv(profile.ProfileEnvVar) // don't care about errors
    62  	path = profile.ProfilePath()
    63  	if !strings.HasPrefix(path, home) {
    64  		t.Error("Unexpected ProfilePath():")
    65  		t.Logf("Expected prefix:  '%s'", home)
    66  		t.Logf("Actual full path: '%s'", path)
    67  	}
    68  
    69  	// set env vars (custom paths)
    70  
    71  	if err := os.Setenv(profile.PreloadEnvVar, temp); err != nil {
    72  		t.Errorf("Unable to set env var %s: %s", profile.PreloadEnvVar, err.Error())
    73  	}
    74  	path = profile.PreloadPath()
    75  	if !strings.HasPrefix(path, temp) {
    76  		t.Error("Unexpected PreloadPath():")
    77  		t.Logf("Expected prefix:  '%s'", temp)
    78  		t.Logf("Actual full path: '%s'", path)
    79  	}
    80  
    81  	if err := os.Setenv(profile.ModuleEnvVar, temp); err != nil {
    82  		t.Errorf("Unable to set env var %s: %s", profile.ModuleEnvVar, err.Error())
    83  	}
    84  	path = profile.ModulePath()
    85  	if !strings.HasPrefix(path, temp) {
    86  		t.Error("Unexpected ModulePath():")
    87  		t.Logf("Expected prefix:  '%s'", temp)
    88  		t.Logf("Actual full path: '%s'", path)
    89  	}
    90  
    91  	if err := os.Setenv(profile.ProfileEnvVar, temp); err != nil {
    92  		t.Errorf("Unable to set env var %s: %s", profile.ProfileEnvVar, err.Error())
    93  	}
    94  	path = profile.ProfilePath()
    95  	if !strings.HasPrefix(path, temp) {
    96  		t.Error("Unexpected ProfilePath():")
    97  		t.Logf("Expected prefix:  '%s'", temp)
    98  		t.Logf("Actual full path: '%s'", path)
    99  	}
   100  
   101  	// set env vars (exact custom file names)
   102  
   103  	if err := os.Setenv(profile.PreloadEnvVar, temp+"foobar"); err != nil {
   104  		t.Errorf("Unable to set env var %s: %s", profile.PreloadEnvVar, err.Error())
   105  	}
   106  	path = profile.PreloadPath()
   107  	if path != temp+"foobar" {
   108  		t.Error("Unexpected PreloadPath():")
   109  		t.Logf("Expected path:  '%s'", temp+"foobar")
   110  		t.Logf("Actual path:    '%s'", path)
   111  	}
   112  
   113  	if err := os.Setenv(profile.ProfileEnvVar, temp+"foobar"); err != nil {
   114  		t.Errorf("Unable to set env var %s: %s", profile.ProfileEnvVar, err.Error())
   115  	}
   116  	path = profile.ProfilePath()
   117  	if path != temp+"foobar" {
   118  		t.Error("Unexpected ProfilePath():")
   119  		t.Logf("Expected path:  '%s'", temp+"foobar")
   120  		t.Logf("Actual path:    '%s'", path)
   121  	}
   122  
   123  	// as above but negative test
   124  
   125  	if err := os.Setenv(profile.PreloadEnvVar, temp); err != nil {
   126  		t.Errorf("Unable to set env var %s: %s", profile.PreloadEnvVar, err.Error())
   127  	}
   128  	path = profile.PreloadPath()
   129  	if path == temp {
   130  		t.Error("Unexpected PreloadPath():")
   131  		t.Logf("Expected prefix:  '%s'", temp)
   132  		t.Logf("Actual full path: '%s'", path)
   133  	}
   134  
   135  	if err := os.Setenv(profile.ProfileEnvVar, temp); err != nil {
   136  		t.Errorf("Unable to set env var %s: %s", profile.ProfileEnvVar, err.Error())
   137  	}
   138  	path = profile.ProfilePath()
   139  	if path == temp+"foobar" {
   140  		t.Error("Unexpected ProfilePath():")
   141  		t.Logf("Expected prefix:  '%s'", temp)
   142  		t.Logf("Actual full path: '%s'", path)
   143  	}
   144  }
   145  
   146  func TestProfileAndCustomPaths(t *testing.T) {
   147  	var (
   148  		preloadFileName = "preload_TestProfileAndCustomPaths.mx"
   149  		modulesPathName = "modules_TestProfileAndCustomPaths.d" // test needs to exclude trailing slash!
   150  		profileFileName = "profile_TestProfileAndCustomPaths.mx"
   151  	)
   152  
   153  	path := t.TempDir()
   154  
   155  	// get running settings
   156  
   157  	bakPreload := os.Getenv(profile.PreloadEnvVar)
   158  	bakModule := os.Getenv(profile.ModuleEnvVar)
   159  	bakProfile := os.Getenv(profile.ProfileEnvVar)
   160  
   161  	defer func() {
   162  		if err := os.Setenv(profile.PreloadEnvVar, bakPreload); err != nil {
   163  			t.Errorf("Unable to restore env var settings: '%s' to '%s'", profile.PreloadEnvVar, bakPreload)
   164  		}
   165  
   166  		if err := os.Setenv(profile.ModuleEnvVar, bakModule); err != nil {
   167  			t.Errorf("Unable to restore env var settings: '%s' to '%s'", profile.ModuleEnvVar, bakModule)
   168  		}
   169  
   170  		if err := os.Setenv(profile.ProfileEnvVar, bakProfile); err != nil {
   171  			t.Errorf("Unable to restore env var settings: '%s' to '%s'", profile.ProfileEnvVar, bakProfile)
   172  		}
   173  	}()
   174  
   175  	// set env vars
   176  
   177  	if err := os.Setenv(profile.PreloadEnvVar, path+preloadFileName); err != nil {
   178  		t.Errorf("Unable to set env var %s: %s", profile.PreloadEnvVar, err.Error())
   179  	}
   180  
   181  	if err := os.Setenv(profile.ModuleEnvVar, path+modulesPathName); err != nil {
   182  		t.Errorf("Unable to set env var %s: %s", profile.ModuleEnvVar, err.Error())
   183  	}
   184  
   185  	if err := os.Setenv(profile.ProfileEnvVar, path+profileFileName); err != nil {
   186  		t.Errorf("Unable to set env var %s: %s", profile.ProfileEnvVar, err.Error())
   187  	}
   188  
   189  	// initialize preload
   190  
   191  	file, err := os.OpenFile(path+preloadFileName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0640)
   192  	if err != nil {
   193  		t.Fatalf("Error initializing %s: %s", preloadFileName, err.Error())
   194  	}
   195  
   196  	_, err = file.WriteString("function: test_preload {}\n")
   197  	if err != nil {
   198  		t.Fatalf("Error initializing %s: %s", preloadFileName, err.Error())
   199  	}
   200  
   201  	if file.Close() != nil {
   202  		t.Fatalf("Error closing %s: %s", preloadFileName, err.Error())
   203  	}
   204  
   205  	// initialize profile
   206  
   207  	file, err = os.OpenFile(path+profileFileName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0640)
   208  	if err != nil {
   209  		t.Fatalf("Error initializing %s: %s", profileFileName, err.Error())
   210  	}
   211  
   212  	_, err = file.WriteString("function: test_profile {}\n")
   213  	if err != nil {
   214  		t.Fatalf("Error initializing %s: %s", profileFileName, err.Error())
   215  	}
   216  
   217  	if file.Close() != nil {
   218  		t.Fatalf("Error closing %s: %s", profileFileName, err.Error())
   219  	}
   220  
   221  	// run tests
   222  
   223  	count.Tests(t, 5)
   224  
   225  	lang.InitEnv()
   226  	profile.Execute(profile.F_PRELOAD | profile.F_MODULES | profile.F_PROFILE)
   227  
   228  	filename := path + modulesPathName
   229  	fi, err := os.Stat(filename)
   230  	if err != nil {
   231  		t.Errorf("Unable to stat '%s': %s", filename, err.Error())
   232  	}
   233  	if !fi.IsDir() {
   234  		t.Errorf("Modules path is not a directory: '%s'", filename)
   235  	}
   236  
   237  	filename = path + modulesPathName + consts.PathSlash + "packages.json"
   238  	_, err = os.Stat(filename)
   239  	if err != nil {
   240  		t.Errorf("Unable to stat '%s': %s", filename, err.Error())
   241  	}
   242  
   243  	filename = path + modulesPathName + consts.PathSlash + "disabled.json"
   244  	_, err = os.Stat(filename)
   245  	if err != nil {
   246  		t.Errorf("Unable to stat '%s': %s", filename, err.Error())
   247  	}
   248  
   249  	if !lang.MxFunctions.Exists("test_preload") {
   250  		t.Errorf("test_preload failed to be defined. Reason: unknown")
   251  		t.Logf("  %v", lang.MxFunctions.Dump())
   252  	}
   253  
   254  	if !lang.MxFunctions.Exists("test_profile") {
   255  		t.Errorf("test_profile failed to be defined. Reason: unknown")
   256  		t.Logf("  %v", lang.MxFunctions.Dump())
   257  	}
   258  }