// Copyright 2024 Matthew Rich . All rights reserved. package resource import ( "context" _ "fmt" "github.com/stretchr/testify/assert" "log" "os" "os/user" "os/exec" "path/filepath" "testing" ) var TempDir string var ProcessTestUserName string var ProcessTestGroupName string func TestMain(m *testing.M) { var err error TempDir, err = os.MkdirTemp("", "testresourcefile") if err != nil || TempDir == "" { log.Fatal(err) } ProcessTestUserName, ProcessTestGroupName = ProcessUserName() rc := m.Run() os.RemoveAll(TempDir) os.Exit(rc) } func ProcessUserName() (string, string) { processUser, userErr := user.Current() if userErr != nil { panic(userErr) } processGroup, groupErr := user.LookupGroupId(processUser.Gid) if groupErr != nil { panic(groupErr) } return processUser.Username, processGroup.Name } func ExitError(e error) string { if e != nil { switch v := e.(type) { case *exec.ExitError: return string(v.Stderr) default: return e.Error() } } return "" } func TestNewResource(t *testing.T) { resourceUri := "file://foo" testFile := NewResource(resourceUri) assert.NotNil(t, testFile) assert.Equal(t, "foo", testFile.(*File).Path) } func TestResolveId(t *testing.T) { testFile := NewResource("file://../../README.md") assert.NotNil(t, testFile) testFile.(*File).normalizePath = true absolutePath, e := filepath.Abs("../../README.md") assert.Nil(t, e) testFile.ResolveId(context.Background()) assert.Equal(t, absolutePath, testFile.(*File).Path) }