fork/gdu
git clone https://git.julia.blue/fork/gdu
d062e88
subvolume
1package analyze 2 3import ( 4"bytes" 5"testing" 6"time" 7 8"github.com/dundee/gdu/v5/pkg/fs" 9 log"github.com/sirupsen/logrus" 10"github.com/stretchr/testify/assert" 11) 12 13func init () { 14log .SetLevel (log .WarnLevel ) 15} 16 17func TestEncode (t * testing.T ) { 18dir := & Dir { 19File :& File { 20Name :"test_dir" , 21Size :10 , 22Usage :18 , 23Mtime :time .Date (2021 ,8 ,19 ,0 ,40 ,0 ,0 ,time .UTC ), 24 }, 25ItemCount :4 , 26BasePath :"." , 27 } 28 29subdir := & Dir { 30File :& File { 31Name :"nested" , 32Size :9 , 33Usage :14 , 34Parent :dir , 35 }, 36ItemCount :3 , 37 } 38file := & File { 39Name :"file2" , 40Size :3 , 41Usage :4 , 42Parent :subdir , 43 } 44file2 := & File { 45Name :"file" , 46Size :5 , 47Usage :6 , 48Parent :subdir , 49Flag :'@' , 50Mtime :time .Date (2021 ,8 ,19 ,0 ,40 ,0 ,0 ,time .UTC ), 51 } 52file3 := & File { 53Name :"file3" , 54Mli :1234 , 55Flag :'H' , 56 } 57dir .Files = fs.Files {subdir } 58subdir .Files = fs.Files {file ,file2 ,file3 } 59 60var buff bytes.Buffer 61err := dir .EncodeJSON (& buff ,true ,nil ) 62 63assert .Nil (t ,err ) 64assert .Contains (t ,buff .String (),`"name":"nested"` ) 65assert .Contains (t ,buff .String (),`"mtime":1629333600` ) 66assert .Contains (t ,buff .String (),`"ino":1234` ) 67assert .Contains (t ,buff .String (),`"hlnkc":true` ) 68assert .Contains (t ,buff .String (),`"asize":10,"dsize":18,"items":4` ) 69assert .Contains (t ,buff .String (),`"name":"nested","asize":9,"dsize":14,"items":3` ) 70} 71 72func TestEncodeEmptyDir (t * testing.T ) { 73dir := & Dir {File :& File {Name :"empty" }} 74 75var buff bytes.Buffer 76err := dir .EncodeJSON (& buff ,false ,nil ) 77 78assert .NoError (t ,err ) 79assert .Equal (t ,"[{\"name\":\"empty\",\"asize\":0,\"dsize\":0,\"items\":0}\n]" ,buff .String ()) 80} 81 82func TestEncodeSelectedAttributes (t * testing.T ) { 83dir := & Dir { 84File :& File { 85Name :"test_dir" , 86Size :10 , 87Usage :18 , 88Mtime :time .Date (2021 ,8 ,19 ,0 ,40 ,0 ,0 ,time .UTC ), 89 }, 90BasePath :"." , 91 } 92dir .AddFile (& File { 93Name :"file" , 94Size :3 , 95Usage :4 , 96Mtime :time .Date (2021 ,8 ,19 ,0 ,40 ,0 ,0 ,time .UTC ), 97Flag :'@' , 98 }) 99 100var buff bytes.Buffer 101err := dir .EncodeJSON (& buff ,true , fs.JSONAttributes {"asize" : {},"dsize" : {}}) 102 103assert .NoError (t ,err ) 104assert .Contains (t ,buff .String (),`"name":"test_dir","asize":10,"dsize":18` ) 105assert .Contains (t ,buff .String (),`"name":"file","asize":3,"dsize":4` ) 106assert .NotContains (t ,buff .String (),`"mtime"` ) 107assert .NotContains (t ,buff .String (),`"notreg"` ) 108}