// 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" "decl/internal/tempdir" ) var TempDir tempdir.Path = "testresourcefile" var ProcessTestUserName string var ProcessTestGroupName string func TestMain(m *testing.M) { err := TempDir.Create() if err != nil || TempDir == "" { log.Fatal(err) } RegisterConverterMocks() ProcessTestUserName, ProcessTestGroupName = ProcessUserName() rc := m.Run() TempDir.Remove() 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) }