This commit is contained in:
Mark Bates 2019-08-31 21:13:35 -04:00
parent aea42c035f
commit 91ee10f16b
12 changed files with 12 additions and 514 deletions

View File

@ -1,91 +0,0 @@
package hdfs
import (
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Create(t *testing.T) {
r := require.New(t)
fs := NewFS()
f, err := fs.Create("/hello.txt")
r.NoError(err)
r.NotNil(f)
fi, err := f.Stat()
r.NoError(err)
r.Equal("/hello.txt", fi.Name())
r.Equal(os.FileMode(0666), fi.Mode())
r.NotZero(fi.ModTime())
}
func Test_Create_Write(t *testing.T) {
r := require.New(t)
fs := NewFS()
f, err := fs.Create("/hello.txt")
r.NoError(err)
r.NotNil(f)
fi, err := f.Stat()
r.NoError(err)
r.Zero(fi.Size())
r.Equal("/hello.txt", fi.Name())
mt := fi.ModTime()
r.NotZero(mt)
sz, err := io.Copy(f, strings.NewReader(radio))
r.NoError(err)
r.Equal(int64(1381), sz)
r.NoError(f.Close())
r.Equal(int64(1381), fi.Size())
r.NotZero(fi.ModTime())
r.NotEqual(mt, fi.ModTime())
}
const radio = `I was tuning in the shine on the late night dial
Doing anything my radio advised
With every one of those late night stations
Playing songs bringing tears to my eyes
I was seriously thinking about hiding the receiver
When the switch broke 'cause it's old
They're saying things that I can hardly believe
They really think we're getting out of control
Radio is a sound salvation
Radio is cleaning up the nation
They say you better listen to the voice of reason
But they don't give you any choice 'cause they think that it's treason
So you had better do as you are told
You better listen to the radio
I wanna bite the hand that feeds me
I wanna bite that hand so badly
I want to make them wish they'd never seen me
Some of my friends sit around every evening
And they worry about the times ahead
But everybody else is overwhelmed by indifference
And the promise of an early bed
You either shut up or get cut up; they don't wanna hear about it
It's only inches on the reel-to-reel
And the radio is in the hands of such a lot of fools
Tryin' to anesthetize the way that you feel
Radio is a sound salvation
Radio is cleaning up the nation
They say you better listen to the voice of reason
But they don't give you any choice 'cause they think that it's treason
So you had better do as you are told
You better listen to the radio
Wonderful radio
Marvelous radio
Wonderful radio
Radio, radio`

View File

@ -3,6 +3,7 @@ package hdfs
import ( import (
"net/http" "net/http"
"os" "os"
"strings"
"github.com/markbates/pkger/fs" "github.com/markbates/pkger/fs"
"github.com/markbates/pkger/here" "github.com/markbates/pkger/here"
@ -20,12 +21,16 @@ type File struct {
} }
func NewFile(fx fs.FileSystem, osf *os.File) (*File, error) { func NewFile(fx fs.FileSystem, osf *os.File) (*File, error) {
info, err := osf.Stat()
cur, err := fx.Current()
if err != nil { if err != nil {
return nil, err return nil, err
} }
pt := fs.Path{
Name: strings.TrimPrefix(osf.Name(), cur.Dir),
}
pt, err := fx.Parse(info.Name()) info, err := osf.Stat()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,67 +0,0 @@
package hdfs
import (
"bytes"
"io"
"io/ioutil"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func Test_File_Read_Memory(t *testing.T) {
r := require.New(t)
fs := NewFS()
f, err := fs.Create("/file_test.go")
r.NoError(err)
_, err = io.Copy(f, bytes.NewReader([]byte("hi!")))
r.NoError(err)
r.NoError(f.Close())
f, err = fs.Open("/file_test.go")
r.NoError(err)
fi, err := f.Stat()
r.NoError(err)
r.Equal("/file_test.go", fi.Name())
b, err := ioutil.ReadAll(f)
r.NoError(err)
r.Equal(string(b), "hi!")
}
func Test_File_Write(t *testing.T) {
r := require.New(t)
fs := NewFS()
f, err := fs.Create("/hello.txt")
r.NoError(err)
r.NotNil(f)
fi, err := f.Stat()
r.NoError(err)
r.Zero(fi.Size())
r.Equal("/hello.txt", fi.Name())
mt := fi.ModTime()
r.NotZero(mt)
sz, err := io.Copy(f, strings.NewReader(radio))
r.NoError(err)
r.Equal(int64(1381), sz)
// because windows can't handle the time precisely
// enough, we have to *force* just a smidge of time
// to ensure the two ModTime's are different.
// i know, i hate it too.
time.Sleep(time.Millisecond)
r.NoError(f.Close())
r.Equal(int64(1381), fi.Size())
r.NotZero(fi.ModTime())
r.NotEqual(mt, fi.ModTime())
}

View File

@ -36,6 +36,9 @@ func (fx *FS) Create(name string) (fs.File, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := fx.MkdirAll(filepath.Dir(name), 0755); err != nil {
return nil, err
}
f, err := os.Create(name) f, err := os.Create(name)
if err != nil { if err != nil {
return nil, err return nil, err
@ -66,6 +69,7 @@ func (f *FS) MkdirAll(p string, perm os.FileMode) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Println(">>>TODO fs/hdfs/hdfs.go:73: pp ", p)
return os.MkdirAll(p, perm) return os.MkdirAll(p, perm)
} }
@ -128,26 +132,5 @@ func (f *FS) Walk(p string, wf filepath.WalkFunc) error {
} }
func (f *FS) locate(p string) (string, error) { func (f *FS) locate(p string) (string, error) {
pt, err := f.Parse(p) return f.current.FilePath(p), nil
if err != nil {
return "", err
}
var info here.Info
if pt.Pkg == "." {
info, err = f.Current()
if err != nil {
return "", err
}
pt.Pkg = info.ImportPath
}
if info.IsZero() {
info, err = f.Info(pt.Pkg)
if err != nil {
return "", fmt.Errorf("%s: %s", pt, err)
}
}
fp := filepath.Join(info.Dir, pt.Name)
return fp, nil
} }

View File

@ -1,23 +0,0 @@
package hdfs
import (
"log"
"github.com/markbates/pkger/fs/fstest"
)
func NewFS() *FS {
fs, err := New()
if err != nil {
log.Fatal(err)
}
return fs
}
var Folder = fstest.TestFiles{
"/testdata/hdfs_test/main.go": {Data: []byte("!/testdata/hdfs_test/main.go")},
"/testdata/hdfs_test/public/index.html": {Data: []byte("!/testdata/hdfs_test/public/index.html")},
"/testdata/hdfs_test/public/images/mark.png": {Data: []byte("!/testdata/hdfs_test/public/images/mark.png")},
"/testdata/hdfs_test/templates/a.txt": {Data: []byte("!/testdata/hdfs_test/templates/a.txt")},
"/testdata/hdfs_test/templates/b/b.txt": {Data: []byte("!/testdata/hdfs_test/templates/b/b.txt")},
}

View File

@ -1,84 +0,0 @@
package hdfs
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func Test_HTTP_Dir(t *testing.T) {
r := require.New(t)
fs := NewFS()
r.NoError(Folder.Create(fs))
dir, err := fs.Open("/")
r.NoError(err)
ts := httptest.NewServer(http.FileServer(dir))
defer ts.Close()
res, err := http.Get(ts.URL + "/")
r.NoError(err)
r.Equal(200, res.StatusCode)
b, err := ioutil.ReadAll(res.Body)
r.NoError(err)
r.Contains(string(b), `<a href="/public/images/mark.png">/public/images/mark.png</a>`)
}
func Test_HTTP_File_Memory(t *testing.T) {
r := require.New(t)
fs := NewFS()
r.NoError(Folder.Create(fs))
dir, err := fs.Open("/")
r.NoError(err)
ts := httptest.NewServer(http.FileServer(dir))
defer ts.Close()
res, err := http.Get(ts.URL + "/public/images/mark.png")
r.NoError(err)
r.Equal(200, res.StatusCode)
b, err := ioutil.ReadAll(res.Body)
r.NoError(err)
r.Contains(string(b), `!/public/images/mark.png`)
}
func Test_HTTP_Dir_Memory_StripPrefix(t *testing.T) {
r := require.New(t)
fs := NewFS()
r.NoError(Folder.Create(fs))
dir, err := fs.Open("/public")
r.NoError(err)
defer dir.Close()
ts := httptest.NewServer(http.StripPrefix("/assets/", http.FileServer(dir)))
defer ts.Close()
res, err := http.Get(ts.URL + "/assets/images/mark.png")
r.NoError(err)
r.Equal(200, res.StatusCode)
b, _ := ioutil.ReadAll(res.Body)
// r.NoError(err)
r.Contains(string(b), "!/public/images/mark.png")
res, err = http.Get(ts.URL + "/assets/images/")
r.NoError(err)
r.Equal(200, res.StatusCode)
b, _ = ioutil.ReadAll(res.Body)
// r.NoError(err)
r.Contains(string(b), `<a href="/mark.png">/mark.png</a>`)
r.NotContains(string(b), `/public`)
r.NotContains(string(b), `/images`)
r.NotContains(string(b), `/go.mod`)
}

View File

@ -1,44 +0,0 @@
package hdfs
import (
"encoding/json"
"io"
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_File_JSON(t *testing.T) {
r := require.New(t)
fs := NewFS()
f, err := fs.Create("/radio.radio")
r.NoError(err)
_, err = io.Copy(f, strings.NewReader(radio))
r.NoError(err)
r.NoError(f.Close())
f, err = fs.Open("/radio.radio")
r.NoError(err)
bi, err := f.Stat()
r.NoError(err)
mj, err := json.Marshal(f)
r.NoError(err)
f2 := &File{}
r.NoError(json.Unmarshal(mj, f2))
ai, err := f2.Stat()
r.NoError(err)
r.Equal(bi.Size(), ai.Size())
fd, err := ioutil.ReadAll(f2)
r.NoError(err)
r.Equal(radio, string(fd))
}

View File

@ -1,23 +0,0 @@
package hdfs
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func Test_MkdirAll(t *testing.T) {
r := require.New(t)
fs := NewFS()
err := fs.MkdirAll("/foo/bar/baz", 0755)
r.NoError(err)
fi, err := fs.Stat("/foo/bar/baz")
r.NoError(err)
r.Equal("/foo/bar/baz", fi.Name())
r.Equal(os.FileMode(0755), fi.Mode())
r.True(fi.IsDir())
}

View File

@ -1,32 +0,0 @@
package hdfs
import (
"io"
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Open(t *testing.T) {
r := require.New(t)
fs := NewFS()
_, err := fs.Open("/i.dont.exist")
r.Error(err)
f, err := fs.Create("/i.exist")
r.NoError(err)
_, err = io.Copy(f, strings.NewReader(radio))
r.NoError(err)
r.NoError(f.Close())
f, err = fs.Open("/i.exist")
r.NoError(err)
b, err := ioutil.ReadAll(f)
r.NoError(err)
r.NoError(f.Close())
r.Equal([]byte(radio), b)
}

View File

@ -1,24 +0,0 @@
package hdfs
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_Stat(t *testing.T) {
r := require.New(t)
fs := NewFS()
_, err := fs.Stat("/i.dont.exist")
r.Error(err)
f, err := fs.Create("/i.exist")
r.NoError(err)
r.NoError(f.Close())
fi, err := fs.Stat("/i.exist")
r.NoError(err)
r.Equal("/i.exist", fi.Name())
}

View File

@ -1,67 +0,0 @@
package hdfs
import (
"io"
"os"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Walk(t *testing.T) {
r := require.New(t)
files := []struct {
name string
body string
}{
{name: "/a/a.txt", body: "A"},
{name: "/a/a.md", body: "Amd"},
{name: "/b/c/d.txt", body: "B"},
{name: "/f.txt", body: "F"},
}
sort.Slice(files, func(a, b int) bool {
return files[a].name < files[b].name
})
fs := NewFS()
for _, file := range files {
f, err := fs.Create(file.name)
r.NoError(err)
_, err = io.Copy(f, strings.NewReader(file.body))
r.NoError(err)
r.NoError(f.Close())
}
var found []string
err := fs.Walk("/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
found = append(found, path)
return nil
})
r.NoError(err)
expected := []string{":/", ":/a", ":/a/a.md", ":/a/a.txt", ":/b", ":/b/c", ":/b/c/d.txt", ":/f.txt"}
r.Equal(expected, found)
found = []string{}
err = fs.Walk("/a/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
found = append(found, path)
return nil
})
r.NoError(err)
expected = []string{":/a/a.md", ":/a/a.txt"}
r.Equal(expected, found)
}

View File

@ -1,35 +0,0 @@
I was tuning in the shine on the late night dial
Doing anything my radio advised
With every one of those late night stations
Playing songs bringing tears to my eyes
I was seriously thinking about hiding the receiver
When the switch broke 'cause it's old
They're saying things that I can hardly believe
They really think we're getting out of control
Radio is a sound salvation
Radio is cleaning up the nation
They say you better listen to the voice of reason
But they don't give you any choice 'cause they think that it's treason
So you had better do as you are told
You better listen to the radio
I wanna bite the hand that feeds me
I wanna bite that hand so badly
I want to make them wish they'd never seen me
Some of my friends sit around every evening
And they worry about the times ahead
But everybody else is overwhelmed by indifference
And the promise of an early bed
You either shut up or get cut up; they don't wanna hear about it
It's only inches on the reel-to-reel
And the radio is in the hands of such a lot of fools
Tryin' to anesthetize the way that you feel
Radio is a sound salvation
Radio is cleaning up the nation
They say you better listen to the voice of reason
But they don't give you any choice 'cause they think that it's treason
So you had better do as you are told
You better listen to the radio
Wonderful radio
Marvelous radio
Wonderful radio
Radio, radio