jx/internal/transport/transport_test.go

94 lines
1.9 KiB
Go
Raw Normal View History

2024-05-12 08:20:51 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package transport
import (
"github.com/stretchr/testify/assert"
"net/url"
"testing"
"fmt"
"os"
"log"
"decl/internal/tempdir"
2024-05-12 08:20:51 +00:00
)
var TempDir tempdir.Path = "testtransportfile"
2024-05-12 08:20:51 +00:00
2024-07-01 07:16:55 +00:00
var testFileResourceDoc string = `
resources:
- type: file
transition: read
attributes:
path: /tmp/foobar
`
2024-05-12 08:20:51 +00:00
func TestMain(m *testing.M) {
2024-10-04 00:12:31 +00:00
err := TempDir.Create()
2024-05-12 08:20:51 +00:00
if err != nil || TempDir == "" {
log.Fatal(err)
}
rc := m.Run()
TempDir.Remove()
2024-05-12 08:20:51 +00:00
os.Exit(rc)
}
func TestNewTransportReader(t *testing.T) {
path := TempDir.FilePath("foo")
2024-05-12 08:20:51 +00:00
u, e := url.Parse(fmt.Sprintf("file://%s", path))
assert.Nil(t, e)
writeErr := os.WriteFile(path, []byte("test"), 0644)
assert.Nil(t, writeErr)
reader, err := NewReader(u)
assert.Nil(t, err)
assert.NotNil(t, reader)
}
2024-07-01 07:16:55 +00:00
func TestTransportReaderContentType(t *testing.T) {
path := TempDir.FilePath("foo.jx.yaml")
2024-07-01 07:16:55 +00:00
u, e := url.Parse(fmt.Sprintf("file://%s", path))
assert.Nil(t, e)
2024-07-17 08:19:55 +00:00
assert.False(t, Exists(u))
2024-07-01 07:16:55 +00:00
writeErr := os.WriteFile(path, []byte(testFileResourceDoc), 0644)
assert.Nil(t, writeErr)
reader, err := NewReader(u)
assert.Nil(t, err)
2024-07-17 08:19:55 +00:00
assert.True(t, reader.Exists())
2024-07-01 07:16:55 +00:00
assert.NotNil(t, reader)
assert.Equal(t, "jx.yaml", reader.ContentType())
2024-07-01 07:16:55 +00:00
}
2024-07-17 08:19:55 +00:00
func TestTransportReaderDir(t *testing.T) {
u, e := url.Parse(fmt.Sprintf("file://%s", string(TempDir)))
assert.Nil(t, e)
reader, err := NewReader(u)
assert.ErrorContains(t, err, "is a directory")
assert.True(t, reader.Exists())
assert.NotNil(t, reader)
}
2024-07-17 08:19:55 +00:00
func TestTransportWriter(t *testing.T) {
path := TempDir.FilePath("writefoo")
2024-07-17 08:19:55 +00:00
u, e := url.Parse(fmt.Sprintf("file://%s", path))
assert.Nil(t, e)
assert.False(t, Exists(u))
writer, err := NewWriter(u)
assert.Nil(t, err)
defer writer.Close()
assert.NotNil(t, writer)
assert.True(t, writer.Exists())
_, writeErr := writer.Write([]byte("testdata"))
assert.Nil(t, writeErr)
assert.FileExists(t, path)
}