jx/internal/resource/resource_test.go
Matthew Rich 2fd1764cce
Some checks failed
Lint / golangci-lint (push) Failing after 10m24s
Declarative Tests / test (push) Failing after 13s
Declarative Tests / build-fedora (push) Successful in 5m5s
Declarative Tests / build-ubuntu-focal (push) Successful in 1m54s
use tempdir pkg to manage test files
2024-09-27 00:54:27 +00:00

81 lines
1.5 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. 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)
}