github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/client/cache/dir_test.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package cache
     7  
     8  import (
     9  	"os"
    10  	"os/user"
    11  	"path"
    12  	"testing"
    13  
    14  	"github.com/sylabs/singularity/internal/pkg/sylog"
    15  )
    16  
    17  var cacheDefault string
    18  
    19  const cacheCustom = "/tmp/customcachedir"
    20  
    21  func TestMain(m *testing.M) {
    22  	usr, err := user.Current()
    23  	if err != nil {
    24  		sylog.Errorf("Couldn't determine user home directory: %v", err)
    25  		os.Exit(1)
    26  	}
    27  	cacheDefault = path.Join(usr.HomeDir, RootDefault)
    28  
    29  	os.Exit(m.Run())
    30  }
    31  
    32  func TestRoot(t *testing.T) {
    33  	tests := []struct {
    34  		name     string
    35  		env      string
    36  		expected string
    37  	}{
    38  		{"Default root", "", cacheDefault},
    39  		{"Custom root", cacheCustom, cacheCustom},
    40  	}
    41  
    42  	for _, tt := range tests {
    43  		t.Run(tt.name, func(t *testing.T) {
    44  			defer Clean()
    45  			defer os.Unsetenv(DirEnv)
    46  
    47  			os.Setenv(DirEnv, tt.env)
    48  
    49  			if r := Root(); r != tt.expected {
    50  				t.Errorf("Unexpected result: %s (expected %s)", r, tt.expected)
    51  			}
    52  		})
    53  	}
    54  }