jx/internal/resource/resource_test.go

81 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-03-20 19:25:25 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
2024-03-20 16:15:27 +00:00
package resource
import (
2024-03-25 20:31:06 +00:00
"context"
_ "fmt"
"github.com/stretchr/testify/assert"
"log"
"os"
2024-05-24 05:11:51 +00:00
"os/user"
"os/exec"
2024-03-25 20:31:06 +00:00
"path/filepath"
"testing"
2024-09-27 00:54:27 +00:00
"decl/internal/tempdir"
2024-03-20 16:15:27 +00:00
)
2024-09-27 00:54:27 +00:00
var TempDir tempdir.Path = "testresourcefile"
2024-03-20 16:15:27 +00:00
2024-05-24 05:11:51 +00:00
var ProcessTestUserName string
var ProcessTestGroupName string
2024-03-20 16:15:27 +00:00
func TestMain(m *testing.M) {
2024-09-27 00:54:27 +00:00
err := TempDir.Create()
2024-03-25 20:31:06 +00:00
if err != nil || TempDir == "" {
log.Fatal(err)
}
RegisterConverterMocks()
2024-05-24 05:11:51 +00:00
ProcessTestUserName, ProcessTestGroupName = ProcessUserName()
2024-03-25 20:31:06 +00:00
rc := m.Run()
2024-09-27 00:54:27 +00:00
TempDir.Remove()
2024-03-25 20:31:06 +00:00
os.Exit(rc)
2024-03-20 16:15:27 +00:00
}
2024-05-24 05:11:51 +00:00
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 ""
}
2024-03-20 19:25:25 +00:00
func TestNewResource(t *testing.T) {
2024-03-25 20:31:06 +00:00
resourceUri := "file://foo"
testFile := NewResource(resourceUri)
assert.NotNil(t, testFile)
2024-03-20 16:15:27 +00:00
2024-03-25 20:31:06 +00:00
assert.Equal(t, "foo", testFile.(*File).Path)
2024-03-20 16:15:27 +00:00
}
2024-03-22 04:35:17 +00:00
func TestResolveId(t *testing.T) {
2024-03-25 20:31:06 +00:00
testFile := NewResource("file://../../README.md")
assert.NotNil(t, testFile)
2024-03-22 04:35:17 +00:00
2024-04-25 07:45:05 +00:00
testFile.(*File).normalizePath = true
2024-03-25 20:31:06 +00:00
absolutePath, e := filepath.Abs("../../README.md")
assert.Nil(t, e)
2024-03-22 04:35:17 +00:00
2024-03-25 20:31:06 +00:00
testFile.ResolveId(context.Background())
assert.Equal(t, absolutePath, testFile.(*File).Path)
2024-03-22 04:35:17 +00:00
}