aboutsummaryrefslogtreecommitdiff
path: root/lib/memfs/memfs_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-07-18 05:19:41 +0700
committerShulhan <ms@kilabit.info>2021-07-25 03:47:52 +0700
commitdab97a3491f28010f81554e717076e1434604b48 (patch)
treeb7ff144082c2024fa1b0d32ac8d988d174b44631 /lib/memfs/memfs_test.go
parent7ba4011107d0c4d214cc7f9d2950f8741da01dc3 (diff)
downloadpakakeh.go-dab97a3491f28010f81554e717076e1434604b48.tar.xz
lib/memfs: implement json.Marshaler on MemFS and Node
Previously, without MarshalJSON, encoding the MemFS or Node object will result in incomplete information, for example, missing name, modification time, and size. This commit implement the json.Marshaler in MemFS which encode the PathNode sorted by keys in ascending order.
Diffstat (limited to 'lib/memfs/memfs_test.go')
-rw-r--r--lib/memfs/memfs_test.go116
1 files changed, 116 insertions, 0 deletions
diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go
index c7b57fbb..83c3c522 100644
--- a/lib/memfs/memfs_test.go
+++ b/lib/memfs/memfs_test.go
@@ -1,6 +1,7 @@
package memfs
import (
+ "encoding/json"
"fmt"
"log"
"os"
@@ -344,6 +345,121 @@ func TestMemFS_Get(t *testing.T) {
}
}
+func TestMemFS_MarshalJSON(t *testing.T) {
+ logp := "MarshalJSON"
+
+ opts := &Options{
+ Root: "testdata/direct/",
+ }
+ mfs, err := New(opts)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ got, err := json.MarshalIndent(mfs, "", "\t")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ exp := `{
+ "/": {
+ "path": "/",
+ "name": "/",
+ "mod_time_epoch": 1569586540,
+ "mod_time_rfc3339": "2019-09-27 12:15:40 +0000 UTC",
+ "mode_string": "drwxr-xr-x",
+ "size": 0,
+ "is_dir": true,
+ "childs": [
+ {
+ "path": "/add",
+ "name": "add",
+ "mod_time_epoch": 1569586540,
+ "mod_time_rfc3339": "2019-09-27 12:15:40 +0000 UTC",
+ "mode_string": "drwxr-xr-x",
+ "size": 0,
+ "is_dir": true,
+ "childs": [
+ {
+ "path": "/add/file",
+ "name": "file",
+ "mod_time_epoch": 1569586540,
+ "mod_time_rfc3339": "2019-09-27 12:15:40 +0000 UTC",
+ "mode_string": "-rw-r--r--",
+ "size": 22,
+ "is_dir": false,
+ "childs": []
+ },
+ {
+ "path": "/add/file2",
+ "name": "file2",
+ "mod_time_epoch": 1569586540,
+ "mod_time_rfc3339": "2019-09-27 12:15:40 +0000 UTC",
+ "mode_string": "-rw-r--r--",
+ "size": 24,
+ "is_dir": false,
+ "childs": []
+ }
+ ]
+ }
+ ]
+ },
+ "/add": {
+ "path": "/add",
+ "name": "add",
+ "mod_time_epoch": 1569586540,
+ "mod_time_rfc3339": "2019-09-27 12:15:40 +0000 UTC",
+ "mode_string": "drwxr-xr-x",
+ "size": 0,
+ "is_dir": true,
+ "childs": [
+ {
+ "path": "/add/file",
+ "name": "file",
+ "mod_time_epoch": 1569586540,
+ "mod_time_rfc3339": "2019-09-27 12:15:40 +0000 UTC",
+ "mode_string": "-rw-r--r--",
+ "size": 22,
+ "is_dir": false,
+ "childs": []
+ },
+ {
+ "path": "/add/file2",
+ "name": "file2",
+ "mod_time_epoch": 1569586540,
+ "mod_time_rfc3339": "2019-09-27 12:15:40 +0000 UTC",
+ "mode_string": "-rw-r--r--",
+ "size": 24,
+ "is_dir": false,
+ "childs": []
+ }
+ ]
+ },
+ "/add/file": {
+ "path": "/add/file",
+ "name": "file",
+ "mod_time_epoch": 1569586540,
+ "mod_time_rfc3339": "2019-09-27 12:15:40 +0000 UTC",
+ "mode_string": "-rw-r--r--",
+ "size": 22,
+ "is_dir": false,
+ "childs": []
+ },
+ "/add/file2": {
+ "path": "/add/file2",
+ "name": "file2",
+ "mod_time_epoch": 1569586540,
+ "mod_time_rfc3339": "2019-09-27 12:15:40 +0000 UTC",
+ "mode_string": "-rw-r--r--",
+ "size": 24,
+ "is_dir": false,
+ "childs": []
+ }
+}`
+
+ test.Assert(t, logp, exp, string(got))
+}
+
func TestMemFS_isIncluded(t *testing.T) {
cases := []struct {
desc string