diff --git a/lib/memfs/internal/test/embed/embed_test.go b/lib/memfs/internal/test/embed/embed_test.go index f36f5638..01dd0133 100644 --- a/lib/memfs/internal/test/embed/embed_test.go +++ b/lib/memfs/internal/test/embed/embed_test.go @@ -101,12 +101,27 @@ func generate_testdata_exclude() *memfs.Node { node.SetModTimeUnix(1633792794, 467899662) node.SetName("exclude") node.SetSize(0) + node.AddChild(_memFS_getNode(memFS, "/exclude/dir", generate_testdata_exclude_dir)) node.AddChild(_memFS_getNode(memFS, "/exclude/index-link.css", generate_testdata_exclude_index_link_css)) node.AddChild(_memFS_getNode(memFS, "/exclude/index-link.html", generate_testdata_exclude_index_link_html)) node.AddChild(_memFS_getNode(memFS, "/exclude/index-link.js", generate_testdata_exclude_index_link_js)) return node } +func generate_testdata_exclude_dir() *memfs.Node { + var node = &memfs.Node{ + SysPath: "testdata/exclude/dir", + Path: "/exclude/dir", + ContentType: "", + GenFuncName: "generate_testdata_exclude_dir", + } + node.SetMode(2147484096) + node.SetModTimeUnix(1629620045, 798024232) + node.SetName("dir") + node.SetSize(0) + return node +} + func generate_testdata_exclude_index_link_css() *memfs.Node { var node = &memfs.Node{ SysPath: "testdata/exclude/index-link.css", @@ -163,12 +178,27 @@ func generate_testdata_include() *memfs.Node { node.SetModTimeUnix(1629623653, 749800911) node.SetName("include") node.SetSize(0) + node.AddChild(_memFS_getNode(memFS, "/include/dir", generate_testdata_include_dir)) node.AddChild(_memFS_getNode(memFS, "/include/index.css", generate_testdata_include_index_css)) node.AddChild(_memFS_getNode(memFS, "/include/index.html", generate_testdata_include_index_html)) node.AddChild(_memFS_getNode(memFS, "/include/index.js", generate_testdata_include_index_js)) return node } +func generate_testdata_include_dir() *memfs.Node { + var node = &memfs.Node{ + SysPath: "testdata/include/dir", + Path: "/include/dir", + ContentType: "", + GenFuncName: "generate_testdata_include_dir", + } + node.SetMode(2147484096) + node.SetModTimeUnix(1564836597, 0) + node.SetName("dir") + node.SetSize(0) + return node +} + func generate_testdata_include_index_css() *memfs.Node { var node = &memfs.Node{ SysPath: "testdata/include/index.css", @@ -288,7 +318,7 @@ func init() { memFS = &memfs.MemFS{ PathNodes: memfs.NewPathNode(), Opts: &memfs.Options{ - Root: "testdata", + Root: "testdata", MaxFileSize: 5242880, Includes: []string{ }, @@ -297,7 +327,7 @@ func init() { `.*/node_save$`, }, Embed: memfs.EmbedOptions{ - CommentHeader: `// SPDX-FileCopyrightText: 2022 Shulhan + CommentHeader: `// SPDX-FileCopyrightText: 2022 Shulhan // SPDX-License-Identifier: AGPL-3.0-or-later `, PackageName: "embed", @@ -319,6 +349,8 @@ func init() { _memFS_getNode(memFS, "/direct/add/file2", generate_testdata_direct_add_file2)) memFS.PathNodes.Set("/exclude", _memFS_getNode(memFS, "/exclude", generate_testdata_exclude)) + memFS.PathNodes.Set("/exclude/dir", + _memFS_getNode(memFS, "/exclude/dir", generate_testdata_exclude_dir)) memFS.PathNodes.Set("/exclude/index-link.css", _memFS_getNode(memFS, "/exclude/index-link.css", generate_testdata_exclude_index_link_css)) memFS.PathNodes.Set("/exclude/index-link.html", @@ -327,6 +359,8 @@ func init() { _memFS_getNode(memFS, "/exclude/index-link.js", generate_testdata_exclude_index_link_js)) memFS.PathNodes.Set("/include", _memFS_getNode(memFS, "/include", generate_testdata_include)) + memFS.PathNodes.Set("/include/dir", + _memFS_getNode(memFS, "/include/dir", generate_testdata_include_dir)) memFS.PathNodes.Set("/include/index.css", _memFS_getNode(memFS, "/include/index.css", generate_testdata_include_index_css)) memFS.PathNodes.Set("/include/index.html", @@ -343,4 +377,9 @@ func init() { _memFS_getNode(memFS, "/plain", generate_testdata_plain)) memFS.Root = memFS.PathNodes.Get("/") + + var err = memFS.Init() + if err != nil { + panic("memFS: " + err.Error()) + } } diff --git a/lib/memfs/internal/test/embed/memfs_embed_test.go b/lib/memfs/internal/test/embed/memfs_embed_test.go index 5cd447d4..a7a5d789 100644 --- a/lib/memfs/internal/test/embed/memfs_embed_test.go +++ b/lib/memfs/internal/test/embed/memfs_embed_test.go @@ -101,6 +101,7 @@ func TestNode_Readdir(t *testing.T) { }, { path: "/exclude", exp: []string{ + "dir", "index-link.css", "index-link.html", "index-link.js", @@ -108,6 +109,7 @@ func TestNode_Readdir(t *testing.T) { }, { path: "/include", exp: []string{ + "dir", "index.css", "index.html", "index.js", diff --git a/lib/memfs/internal/test/embed_disable_modtime/embed_test.go b/lib/memfs/internal/test/embed_disable_modtime/embed_test.go index dcdc562b..05bb83ed 100644 --- a/lib/memfs/internal/test/embed_disable_modtime/embed_test.go +++ b/lib/memfs/internal/test/embed_disable_modtime/embed_test.go @@ -93,12 +93,26 @@ func generate_testdata_exclude() *memfs.Node { node.SetMode(2147484141) node.SetName("exclude") node.SetSize(0) + node.AddChild(_memFS_getNode(memFS, "/exclude/dir", generate_testdata_exclude_dir)) node.AddChild(_memFS_getNode(memFS, "/exclude/index-link.css", generate_testdata_exclude_index_link_css)) node.AddChild(_memFS_getNode(memFS, "/exclude/index-link.html", generate_testdata_exclude_index_link_html)) node.AddChild(_memFS_getNode(memFS, "/exclude/index-link.js", generate_testdata_exclude_index_link_js)) return node } +func generate_testdata_exclude_dir() *memfs.Node { + var node = &memfs.Node{ + SysPath: "testdata/exclude/dir", + Path: "/exclude/dir", + ContentType: "", + GenFuncName: "generate_testdata_exclude_dir", + } + node.SetMode(2147484096) + node.SetName("dir") + node.SetSize(0) + return node +} + func generate_testdata_exclude_index_link_css() *memfs.Node { var node = &memfs.Node{ SysPath: "testdata/exclude/index-link.css", @@ -151,12 +165,26 @@ func generate_testdata_include() *memfs.Node { node.SetMode(2147484141) node.SetName("include") node.SetSize(0) + node.AddChild(_memFS_getNode(memFS, "/include/dir", generate_testdata_include_dir)) node.AddChild(_memFS_getNode(memFS, "/include/index.css", generate_testdata_include_index_css)) node.AddChild(_memFS_getNode(memFS, "/include/index.html", generate_testdata_include_index_html)) node.AddChild(_memFS_getNode(memFS, "/include/index.js", generate_testdata_include_index_js)) return node } +func generate_testdata_include_dir() *memfs.Node { + var node = &memfs.Node{ + SysPath: "testdata/include/dir", + Path: "/include/dir", + ContentType: "", + GenFuncName: "generate_testdata_include_dir", + } + node.SetMode(2147484096) + node.SetName("dir") + node.SetSize(0) + return node +} + func generate_testdata_include_index_css() *memfs.Node { var node = &memfs.Node{ SysPath: "testdata/include/index.css", @@ -269,7 +297,7 @@ func init() { memFS = &memfs.MemFS{ PathNodes: memfs.NewPathNode(), Opts: &memfs.Options{ - Root: "testdata", + Root: "testdata", MaxFileSize: 5242880, Includes: []string{ }, @@ -278,7 +306,7 @@ func init() { `.*/node_save$`, }, Embed: memfs.EmbedOptions{ - CommentHeader: ``, + CommentHeader: ``, PackageName: "embed", VarName: "memFS", GoFileName: "./internal/test/embed_disable_modtime/embed_test.go", @@ -298,6 +326,8 @@ func init() { _memFS_getNode(memFS, "/direct/add/file2", generate_testdata_direct_add_file2)) memFS.PathNodes.Set("/exclude", _memFS_getNode(memFS, "/exclude", generate_testdata_exclude)) + memFS.PathNodes.Set("/exclude/dir", + _memFS_getNode(memFS, "/exclude/dir", generate_testdata_exclude_dir)) memFS.PathNodes.Set("/exclude/index-link.css", _memFS_getNode(memFS, "/exclude/index-link.css", generate_testdata_exclude_index_link_css)) memFS.PathNodes.Set("/exclude/index-link.html", @@ -306,6 +336,8 @@ func init() { _memFS_getNode(memFS, "/exclude/index-link.js", generate_testdata_exclude_index_link_js)) memFS.PathNodes.Set("/include", _memFS_getNode(memFS, "/include", generate_testdata_include)) + memFS.PathNodes.Set("/include/dir", + _memFS_getNode(memFS, "/include/dir", generate_testdata_include_dir)) memFS.PathNodes.Set("/include/index.css", _memFS_getNode(memFS, "/include/index.css", generate_testdata_include_index_css)) memFS.PathNodes.Set("/include/index.html", @@ -322,4 +354,9 @@ func init() { _memFS_getNode(memFS, "/plain", generate_testdata_plain)) memFS.Root = memFS.PathNodes.Get("/") + + var err = memFS.Init() + if err != nil { + panic("memFS: " + err.Error()) + } } diff --git a/lib/memfs/internal/test/embed_disable_modtime/memfs_embed_test.go b/lib/memfs/internal/test/embed_disable_modtime/memfs_embed_test.go index 5cd447d4..a7a5d789 100644 --- a/lib/memfs/internal/test/embed_disable_modtime/memfs_embed_test.go +++ b/lib/memfs/internal/test/embed_disable_modtime/memfs_embed_test.go @@ -101,6 +101,7 @@ func TestNode_Readdir(t *testing.T) { }, { path: "/exclude", exp: []string{ + "dir", "index-link.css", "index-link.html", "index-link.js", @@ -108,6 +109,7 @@ func TestNode_Readdir(t *testing.T) { }, { path: "/include", exp: []string{ + "dir", "index.css", "index.html", "index.js", diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go index e6f30499..7bea720e 100644 --- a/lib/memfs/memfs.go +++ b/lib/memfs/memfs.go @@ -537,7 +537,7 @@ func (mfs *MemFS) mount() (err error) { return nil } if mfs.Root != nil { - // The directory has been initialized by embedded. + // The Root node may has been initialized through embedding. return nil } @@ -568,17 +568,15 @@ func (mfs *MemFS) Remount() (err error) { return mfs.mount() } -// scanDir scan the directory node for files and add them to memory file -// system. +// scanDir scan the content of node directory and add them to mfs. // It returns number of childs added to the node or an error. func (mfs *MemFS) scanDir(node *Node) (n int, err error) { var ( - logp = "scanDir" - child *Node - nchilds int - f *os.File - fi os.FileInfo - fis []os.FileInfo + logp = "scanDir" + child *Node + f *os.File + fi os.FileInfo + fis []os.FileInfo ) f, err = os.Open(node.SysPath) @@ -613,16 +611,11 @@ func (mfs *MemFS) scanDir(node *Node) (n int, err error) { continue } - nchilds, err = mfs.scanDir(child) + _, err = mfs.scanDir(child) if err != nil { err = fmt.Errorf(`%s %q: %w`, logp, node.SysPath, err) goto out } - if nchilds == 0 { - // No childs added, remove it from node. - mfs.RemoveChild(node, child) - n-- - } } out: errClose := f.Close() diff --git a/lib/memfs/memfs_example_test.go b/lib/memfs/memfs_example_test.go index 43fd037d..c064460f 100644 --- a/lib/memfs/memfs_example_test.go +++ b/lib/memfs/memfs_example_test.go @@ -68,7 +68,7 @@ func ExampleNew() { // Output: // Content of /index.html: - // List of embedded files: [/ /include /include/index.css /include/index.html /include/index.js /index.css /index.html /index.js] + // List of embedded files: [/ /direct /direct/add /include /include/dir /include/index.css /include/index.html /include/index.js /index.css /index.html /index.js] // Error on Get /exclude/index.html: file does not exist } diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index 6d8dbadc..7e210d9e 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -115,10 +115,12 @@ func TestNew(t *testing.T) { expMapKeys: []string{ "/", "/exclude", + "/exclude/dir", "/exclude/index-link.css", "/exclude/index-link.html", "/exclude/index-link.js", "/include", + "/include/dir", "/include/index.css", "/include/index.html", "/include/index.js", @@ -141,9 +143,11 @@ func TestNew(t *testing.T) { expMapKeys: []string{ "/", "/exclude", + "/exclude/dir", "/exclude/index-link.css", "/exclude/index-link.html", "/include", + "/include/dir", "/include/index.css", "/include/index.html", "/index.css", @@ -166,8 +170,10 @@ func TestNew(t *testing.T) { expMapKeys: []string{ "/", "/exclude", + "/exclude/dir", "/exclude/index-link.js", "/include", + "/include/dir", "/include/index.js", "/index.js", }, @@ -669,6 +675,7 @@ func TestMerge(t *testing.T) { Path: "/", Childs: []*Node{ mfsDirect.MustGet("/add"), + mfsInclude.MustGet("/dir"), mfsInclude.MustGet("/index.css"), mfsInclude.MustGet("/index.html"), mfsInclude.MustGet("/index.js"), @@ -678,6 +685,7 @@ func TestMerge(t *testing.T) { "/add": mfsDirect.MustGet("/add"), "/add/file": mfsDirect.MustGet("/add/file"), "/add/file2": mfsDirect.MustGet("/add/file2"), + "/dir": mfsInclude.MustGet("/dir"), "/index.css": mfsInclude.MustGet("/index.css"), "/index.html": mfsInclude.MustGet("/index.html"), "/index.js": mfsInclude.MustGet("/index.js"),