Compare commits
No commits in common. "d359d8bfabd85665df773d5154a670477f96a5c8" and "eaaf0f89317e160adb58c6655772d06e00bac8a4" have entirely different histories.
d359d8bfab
...
eaaf0f8931
@ -1,31 +0,0 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
package ext
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type Closer func() error
|
||||
|
||||
func WriteAddCloser(w io.WriteCloser, c Closer) io.WriteCloser {
|
||||
a := writeAddCloser{ WriteCloser: w, AddClose: func() (err error) {
|
||||
if err = w.Close(); err != nil {
|
||||
return
|
||||
}
|
||||
if c != nil {
|
||||
return c()
|
||||
}
|
||||
return
|
||||
} }
|
||||
return a
|
||||
}
|
||||
|
||||
type writeAddCloser struct {
|
||||
io.WriteCloser
|
||||
AddClose Closer
|
||||
}
|
||||
|
||||
func (w writeAddCloser) Close() error {
|
||||
return w.AddClose()
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
package ext
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"strings"
|
||||
_ "fmt"
|
||||
_ "log"
|
||||
)
|
||||
|
||||
func TestNewWriteAddCloser(t *testing.T) {
|
||||
var testWriter strings.Builder
|
||||
|
||||
closer := WriteAddCloser(WriteNopCloser(&testWriter), func() error { testWriter.Write([]byte("foo")); return nil })
|
||||
closer.Close()
|
||||
assert.Equal(t, "foo", testWriter.String())
|
||||
}
|
@ -13,7 +13,6 @@ _ "errors"
|
||||
"fmt"
|
||||
"compress/gzip"
|
||||
"log/slog"
|
||||
"decl/internal/ext"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
@ -99,7 +98,6 @@ func NewFileWriter(u *url.URL) (f *FileWriter, err error) {
|
||||
}
|
||||
f.extension()
|
||||
f.DetectGzip()
|
||||
slog.Info("transport.NewFileWriter()", "file", f.File)
|
||||
exists := FileExists(u)
|
||||
slog.Info("transport.NewFileWriter()", "file", f, "error", err, "exists", exists)
|
||||
|
||||
@ -112,7 +110,7 @@ func NewFileWriter(u *url.URL) (f *FileWriter, err error) {
|
||||
}
|
||||
}
|
||||
if f.Gzip() {
|
||||
f.gzipWriter = ext.WriteAddCloser(gzip.NewWriter(f.writeHandle), func() error { return f.writeHandle.Close() })
|
||||
f.gzipWriter = gzip.NewWriter(f.writeHandle)
|
||||
}
|
||||
slog.Info("transport.NewFileWriter()", "file", f, "error", err)
|
||||
return
|
||||
@ -141,7 +139,7 @@ func NewFile(u *url.URL) (f *File, err error) {
|
||||
}
|
||||
|
||||
if f.Gzip() {
|
||||
f.gzipWriter = ext.WriteAddCloser(gzip.NewWriter(f.writeHandle), func() error { return f.writeHandle.Close() })
|
||||
f.gzipWriter = gzip.NewWriter(f.writeHandle)
|
||||
if exists {
|
||||
if f.gzipReader, err = gzip.NewReader(f.readHandle); err != nil {
|
||||
return
|
||||
@ -166,7 +164,7 @@ func (f *File) extension() {
|
||||
}
|
||||
|
||||
func (f *File) DetectGzip() {
|
||||
f.gzip = (f.uri.Query().Get("gzip") == "true" || f.fileext == "gz" || f.exttype == "tgz" || f.exttype == "gz" || f.fileext == "tgz")
|
||||
f.gzip = (f.uri.Query().Get("gzip") == "true" || f.fileext == "gz")
|
||||
}
|
||||
|
||||
func (f *File) URI() *url.URL {
|
||||
|
@ -7,11 +7,8 @@ import (
|
||||
"testing"
|
||||
"fmt"
|
||||
"os"
|
||||
"io"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"compress/gzip"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var TransportFileTestFile = fmt.Sprintf("%s/foo", TempDir)
|
||||
@ -40,35 +37,3 @@ func TestNewTransportFileReaderExtension(t *testing.T) {
|
||||
f.extension()
|
||||
assert.Equal(t, f.exttype, "yaml")
|
||||
}
|
||||
|
||||
// Use transport to write a compressed file then verify it.
|
||||
func TestTransportFileGzipWriter(t *testing.T) {
|
||||
var content strings.Builder
|
||||
var testValue string = "write a compressed plaintext file"
|
||||
|
||||
path := TempDir.FilePath("foo.tgz")
|
||||
u, e := url.Parse(fmt.Sprintf("file://%s", path))
|
||||
assert.Nil(t, e)
|
||||
|
||||
fw, err := NewFileWriter(u)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "tgz", fw.File.exttype)
|
||||
|
||||
assert.True(t, fw.Gzip())
|
||||
|
||||
writer := fw.Writer()
|
||||
writer.Write([]byte(testValue))
|
||||
assert.Nil(t, writer.Close())
|
||||
|
||||
assert.True(t, TempDir.FileExists("foo.tgz"))
|
||||
|
||||
r, openErr := TempDir.Open("foo.tgz")
|
||||
assert.Nil(t, openErr)
|
||||
gzipReader, gzipErr := gzip.NewReader(r)
|
||||
assert.Nil(t, gzipErr)
|
||||
_, readErr := io.Copy(&content, gzipReader)
|
||||
assert.Nil(t, readErr)
|
||||
assert.Equal(t, content.String(), testValue)
|
||||
}
|
||||
|
||||
|
||||
|
@ -19,7 +19,6 @@ type Handler interface {
|
||||
URI() *url.URL
|
||||
ContentType() string
|
||||
SetGzip(bool)
|
||||
DetectGzip()
|
||||
Gzip() bool
|
||||
Signature() string
|
||||
Stat() (fs.FileInfo, error)
|
||||
@ -139,8 +138,6 @@ func (r *Reader) SetGzip(value bool) {
|
||||
r.handle.SetGzip(value)
|
||||
}
|
||||
|
||||
func (r *Reader) DetectGzip() { r.handle.DetectGzip() }
|
||||
|
||||
func (r *Reader) Gzip() bool {
|
||||
return r.handle.Gzip()
|
||||
}
|
||||
@ -199,8 +196,6 @@ func (w *Writer) SetGzip(value bool) {
|
||||
w.handle.SetGzip(value)
|
||||
}
|
||||
|
||||
func (w *Writer) DetectGzip() { w.handle.DetectGzip() }
|
||||
|
||||
func (w *Writer) Gzip() bool {
|
||||
return w.handle.Gzip()
|
||||
}
|
||||
|
@ -9,10 +9,9 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"log"
|
||||
"decl/internal/tempdir"
|
||||
)
|
||||
|
||||
var TempDir tempdir.Path = "testtransportfile"
|
||||
var TempDir string
|
||||
|
||||
var testFileResourceDoc string = `
|
||||
resources:
|
||||
@ -24,19 +23,19 @@ resources:
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
err = TempDir.Create()
|
||||
TempDir, err = os.MkdirTemp("", "testtransportfile")
|
||||
if err != nil || TempDir == "" {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
rc := m.Run()
|
||||
|
||||
TempDir.Remove()
|
||||
os.RemoveAll(TempDir)
|
||||
os.Exit(rc)
|
||||
}
|
||||
|
||||
func TestNewTransportReader(t *testing.T) {
|
||||
path := TempDir.FilePath("foo")
|
||||
path := fmt.Sprintf("%s/foo", TempDir)
|
||||
u, e := url.Parse(fmt.Sprintf("file://%s", path))
|
||||
assert.Nil(t, e)
|
||||
|
||||
@ -49,7 +48,7 @@ func TestNewTransportReader(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTransportReaderContentType(t *testing.T) {
|
||||
path := TempDir.FilePath("foo.jx.yaml")
|
||||
path := fmt.Sprintf("%s/foo.jx.yaml", TempDir)
|
||||
u, e := url.Parse(fmt.Sprintf("file://%s", path))
|
||||
assert.Nil(t, e)
|
||||
|
||||
@ -67,7 +66,7 @@ func TestTransportReaderContentType(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTransportReaderDir(t *testing.T) {
|
||||
u, e := url.Parse(fmt.Sprintf("file://%s", string(TempDir)))
|
||||
u, e := url.Parse(fmt.Sprintf("file://%s", TempDir))
|
||||
assert.Nil(t, e)
|
||||
|
||||
reader, err := NewReader(u)
|
||||
@ -77,7 +76,7 @@ func TestTransportReaderDir(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTransportWriter(t *testing.T) {
|
||||
path := TempDir.FilePath("writefoo")
|
||||
path := fmt.Sprintf("%s/writefoo", TempDir)
|
||||
u, e := url.Parse(fmt.Sprintf("file://%s", path))
|
||||
assert.Nil(t, e)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user