From 91ee10f16bfe7440f32f17da9773d70eba2ea9cc Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 31 Aug 2019 21:13:35 -0400 Subject: [PATCH] wrong --- fs/hdfs/create_test.go | 91 ------------------------------------- fs/hdfs/file.go | 9 +++- fs/hdfs/file_test.go | 67 --------------------------- fs/hdfs/hdfs.go | 27 ++--------- fs/hdfs/hdfs_test.go | 23 ---------- fs/hdfs/http_test.go | 84 ---------------------------------- fs/hdfs/json_test.go | 44 ------------------ fs/hdfs/mkdirall_test.go | 23 ---------- fs/hdfs/open_test.go | 32 ------------- fs/hdfs/stat_test.go | 24 ---------- fs/hdfs/walk_test.go | 67 --------------------------- testdata/public/radio.radio | 35 -------------- 12 files changed, 12 insertions(+), 514 deletions(-) delete mode 100644 fs/hdfs/create_test.go delete mode 100644 fs/hdfs/file_test.go delete mode 100644 fs/hdfs/hdfs_test.go delete mode 100644 fs/hdfs/http_test.go delete mode 100644 fs/hdfs/json_test.go delete mode 100644 fs/hdfs/mkdirall_test.go delete mode 100644 fs/hdfs/open_test.go delete mode 100644 fs/hdfs/stat_test.go delete mode 100644 fs/hdfs/walk_test.go delete mode 100644 testdata/public/radio.radio diff --git a/fs/hdfs/create_test.go b/fs/hdfs/create_test.go deleted file mode 100644 index 6b701b2..0000000 --- a/fs/hdfs/create_test.go +++ /dev/null @@ -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` diff --git a/fs/hdfs/file.go b/fs/hdfs/file.go index 572e4cd..f2b366c 100644 --- a/fs/hdfs/file.go +++ b/fs/hdfs/file.go @@ -3,6 +3,7 @@ package hdfs import ( "net/http" "os" + "strings" "github.com/markbates/pkger/fs" "github.com/markbates/pkger/here" @@ -20,12 +21,16 @@ type File struct { } func NewFile(fx fs.FileSystem, osf *os.File) (*File, error) { - info, err := osf.Stat() + + cur, err := fx.Current() if err != nil { 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 { return nil, err } diff --git a/fs/hdfs/file_test.go b/fs/hdfs/file_test.go deleted file mode 100644 index 7c6ca0f..0000000 --- a/fs/hdfs/file_test.go +++ /dev/null @@ -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()) -} diff --git a/fs/hdfs/hdfs.go b/fs/hdfs/hdfs.go index 9d05773..3d4892d 100644 --- a/fs/hdfs/hdfs.go +++ b/fs/hdfs/hdfs.go @@ -36,6 +36,9 @@ func (fx *FS) Create(name string) (fs.File, error) { if err != nil { return nil, err } + if err := fx.MkdirAll(filepath.Dir(name), 0755); err != nil { + return nil, err + } f, err := os.Create(name) if err != nil { return nil, err @@ -66,6 +69,7 @@ func (f *FS) MkdirAll(p string, perm os.FileMode) error { if err != nil { return err } + fmt.Println(">>>TODO fs/hdfs/hdfs.go:73: pp ", p) 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) { - pt, err := f.Parse(p) - 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 + return f.current.FilePath(p), nil } diff --git a/fs/hdfs/hdfs_test.go b/fs/hdfs/hdfs_test.go deleted file mode 100644 index 6d52926..0000000 --- a/fs/hdfs/hdfs_test.go +++ /dev/null @@ -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")}, -} diff --git a/fs/hdfs/http_test.go b/fs/hdfs/http_test.go deleted file mode 100644 index c97e85f..0000000 --- a/fs/hdfs/http_test.go +++ /dev/null @@ -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), `/public/images/mark.png`) -} - -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), `/mark.png`) - r.NotContains(string(b), `/public`) - r.NotContains(string(b), `/images`) - r.NotContains(string(b), `/go.mod`) -} diff --git a/fs/hdfs/json_test.go b/fs/hdfs/json_test.go deleted file mode 100644 index e569706..0000000 --- a/fs/hdfs/json_test.go +++ /dev/null @@ -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)) -} diff --git a/fs/hdfs/mkdirall_test.go b/fs/hdfs/mkdirall_test.go deleted file mode 100644 index 2506dfc..0000000 --- a/fs/hdfs/mkdirall_test.go +++ /dev/null @@ -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()) -} diff --git a/fs/hdfs/open_test.go b/fs/hdfs/open_test.go deleted file mode 100644 index 2805825..0000000 --- a/fs/hdfs/open_test.go +++ /dev/null @@ -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) -} diff --git a/fs/hdfs/stat_test.go b/fs/hdfs/stat_test.go deleted file mode 100644 index 3ce3eaf..0000000 --- a/fs/hdfs/stat_test.go +++ /dev/null @@ -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()) -} diff --git a/fs/hdfs/walk_test.go b/fs/hdfs/walk_test.go deleted file mode 100644 index 61117e2..0000000 --- a/fs/hdfs/walk_test.go +++ /dev/null @@ -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) -} diff --git a/testdata/public/radio.radio b/testdata/public/radio.radio deleted file mode 100644 index 0509516..0000000 --- a/testdata/public/radio.radio +++ /dev/null @@ -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 \ No newline at end of file