fork/gdu
git clone https://git.julia.blue/fork/gdu
34f3996
master
1package stdout 2 3import ( 4"bytes" 5"os" 6"path/filepath" 7"testing" 8 9"github.com/dundee/gdu/v5/pkg/analyze" 10"github.com/stretchr/testify/assert" 11"github.com/stretchr/testify/require" 12) 13 14func createMultiTestDirs (t * testing.T ) (first ,second string ) { 15t .Helper () 16 17require .NoError (t ,os .MkdirAll ("multi_a/nested" ,os .ModePerm )) 18require .NoError (t ,os .MkdirAll ("multi_b/other" ,os .ModePerm )) 19require .NoError (t ,os .WriteFile ("multi_a/nested/file" , []byte ("hello" ),0o600 )) 20require .NoError (t ,os .WriteFile ("multi_b/other/file2" , []byte ("go" ),0o600 )) 21 22t .Cleanup (func () { 23assert .NoError (t ,os .RemoveAll ("multi_a" )) 24assert .NoError (t ,os .RemoveAll ("multi_b" )) 25 }) 26 27return "multi_a" ,"multi_b" 28} 29 30func TestAnalyzePathsListsEveryRoot (t * testing.T ) { 31first ,second := createMultiTestDirs (t ) 32output := & bytes.Buffer {} 33 34ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 35require .NoError (t ,ui .AnalyzePaths ([]string {first ,second })) 36 37assert .Contains (t ,output .String (),first ) 38assert .Contains (t ,output .String (),second ) 39} 40 41// Roots from different parents can share a base name, so they are listed by 42// absolute path instead. 43func TestAnalyzePathsLabelsRootsWithTheirPath (t * testing.T ) { 44first := filepath .Join ("outer_p" ,"data" ) 45second := filepath .Join ("outer_q" ,"data" ) 46for _ ,dir := range []string {first ,second } { 47require .NoError (t ,os .MkdirAll (dir ,os .ModePerm )) 48require .NoError (t ,os .WriteFile (filepath .Join (dir ,"file" ), []byte ("hello" ),0o600 )) 49 } 50t .Cleanup (func () { 51assert .NoError (t ,os .RemoveAll ("outer_p" )) 52assert .NoError (t ,os .RemoveAll ("outer_q" )) 53 }) 54 55output := & bytes.Buffer {} 56ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 57require .NoError (t ,ui .AnalyzePaths ([]string {first ,second })) 58 59assert .Contains (t ,output .String (),first ) 60assert .Contains (t ,output .String (),second ) 61// the path is not given a second leading separator by the directory marker 62assert .NotContains (t ,output .String (),string (filepath .Separator )+ first ) 63} 64 65func TestAnalyzePathsSummarizeReportsCombinedTotal (t * testing.T ) { 66first ,second := createMultiTestDirs (t ) 67output := & bytes.Buffer {} 68 69ui := CreateStdoutUI (output ,false ,false ,false ,false ,true ,false ,false ,"" ,0 ,false ,0 ) 70require .NoError (t ,ui .AnalyzePaths ([]string {first ,second })) 71 72assert .NotEmpty (t ,output .String ()) 73} 74 75func TestAnalyzePathsSwapsTopDirAnalyzerForFullTree (t * testing.T ) { 76first ,second := createMultiTestDirs (t ) 77output := & bytes.Buffer {} 78 79ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 80// the default stdout analyzer produces SimpleDir, which cannot be re-parented 81_ ,isTopDirAnalyzer := ui .Analyzer .(* analyze.TopDirAnalyzer ) 82require .True (t ,isTopDirAnalyzer ) 83 84require .NoError (t ,ui .AnalyzePaths ([]string {first ,second })) 85 86_ ,stillTopDirAnalyzer := ui .Analyzer .(* analyze.TopDirAnalyzer ) 87assert .False (t ,stillTopDirAnalyzer ) 88} 89 90func TestAnalyzePathsWithSinglePathBehavesLikeAnalyzePath (t * testing.T ) { 91first ,_ := createMultiTestDirs (t ) 92output := & bytes.Buffer {} 93 94ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 95require .NoError (t ,ui .AnalyzePaths ([]string {first })) 96 97// contents of the single root are listed, not the root itself 98assert .Contains (t ,output .String (),"nested" ) 99assert .NotContains (t ,output .String (),analyze .VirtualRootName ) 100}