fork/gdu
git clone https://git.julia.blue/fork/gdu
b279b41
master
1//go:build !windows 2 3package remove 4 5import ( 6"errors" 7"io" 8"os" 9"path/filepath" 10"strings" 11"testing" 12 13"github.com/stretchr/testify/assert" 14"github.com/stretchr/testify/require" 15 16"github.com/dundee/gdu/v5/internal/testdir" 17"github.com/dundee/gdu/v5/pkg/analyze" 18"github.com/dundee/gdu/v5/pkg/fs" 19) 20 21func mockTrashCommandOS (t * testing.T ,configure func (* trashCommandOSOps )) { 22t .Helper () 23original := trashCommandOS 24t .Cleanup (func () {trashCommandOS = original }) 25configure (& trashCommandOS ) 26} 27 28// createTestTree builds the in-memory counterpart of the directory created by 29// testdir.CreateTestDir and returns the dir holding file2 together with the file. 30func createTestTree () (* analyze.Dir ,* analyze.File ) { 31dir := & analyze.Dir { 32File :& analyze.File { 33Name :"test_dir" , 34Size :5 , 35Usage :12 , 36 }, 37ItemCount :3 , 38BasePath :"." , 39 } 40subdir := & analyze.Dir { 41File :& analyze.File { 42Name :"nested" , 43Size :4 , 44Usage :8 , 45Parent :dir , 46 }, 47ItemCount :2 , 48 } 49file := & analyze.File { 50Name :"file2" , 51Size :3 , 52Usage :4 , 53Parent :subdir , 54 } 55dir .Files = fs.Files {subdir } 56subdir .Files = fs.Files {file } 57 58return subdir ,file 59} 60 61func TestTrashCommand (t * testing.T ) { 62fin := testdir .CreateTestDir () 63defer fin () 64 65subdir ,file := createTestTree () 66 67err := TrashCommand ("rm -rf" )(subdir ,file ) 68require .NoError (t ,err ) 69 70_ ,err = os .Stat ("test_dir/nested/file2" ) 71assert .True (t ,os .IsNotExist (err )) 72 73assert .Equal (t ,0 ,len (subdir .Files )) 74assert .Equal (t ,int64 (1 ),subdir .ItemCount ) 75} 76 77func TestTrashCommandOnDir (t * testing.T ) { 78fin := testdir .CreateTestDir () 79defer fin () 80 81dir := & analyze.Dir { 82File :& analyze.File { 83Name :"test_dir" , 84Size :5 , 85Usage :12 , 86 }, 87ItemCount :3 , 88BasePath :"." , 89 } 90subdir := & analyze.Dir { 91File :& analyze.File { 92Name :"nested" , 93Size :4 , 94Usage :8 , 95Parent :dir , 96 }, 97ItemCount :2 , 98 } 99dir .Files = fs.Files {subdir } 100 101err := TrashCommand ("rm -rf" )(dir ,subdir ) 102require .NoError (t ,err ) 103 104_ ,err = os .Stat ("test_dir/nested" ) 105assert .True (t ,os .IsNotExist (err )) 106assert .Equal (t ,0 ,len (dir .Files )) 107} 108 109func TestShellScript (t * testing.T ) { 110tests := []struct { 111name string 112command string 113want string 114 }{ 115 { 116name :"path appended" , 117command :"trash-put" , 118want :`trash-put "$@"` , 119 }, 120 { 121name :"path appended after arguments" , 122command :"trash-put --trash-dir ~/mytrash" , 123want :`trash-put --trash-dir ~/mytrash "$@"` , 124 }, 125 { 126name :"env var is not a placeholder" , 127command :"trash-put --trash-dir $HOME/mytrash" , 128want :`trash-put --trash-dir $HOME/mytrash "$@"` , 129 }, 130 { 131name :"quoted first parameter" , 132command :`mv -f "$1" ~/mytrash/` , 133want :`mv -f "$1" ~/mytrash/` , 134 }, 135 { 136name :"braced first parameter" , 137command :`mv -f "${1}" ~/mytrash/` , 138want :`mv -f "${1}" ~/mytrash/` , 139 }, 140 { 141name :"all parameters" , 142command :`mv -f "$@" ~/mytrash/` , 143want :`mv -f "$@" ~/mytrash/` , 144 }, 145 { 146name :"all parameters as single word" , 147command :`mv -f "$*" ~/mytrash/` , 148want :`mv -f "$*" ~/mytrash/` , 149 }, 150 } 151 152for _ ,tt := range tests { 153t .Run (tt .name ,func (t * testing.T ) { 154assert .Equal (t ,tt .want ,shellScript (tt .command )) 155 }) 156 } 157} 158 159// writeScript creates a shell script with the given body and returns a command 160// running it, so tests can use commands carrying their own arguments. 161func writeScript (t * testing.T ,body string )string { 162t .Helper () 163path := filepath .Join (t .TempDir (),"script.sh" ) 164require .NoError (t ,os .WriteFile (path , []byte (body ),0o600 )) 165return "/bin/sh " + path 166} 167 168// TestTrashCommandWithArguments checks that the path is appended to a command 169// which already carries arguments of its own. 170func TestTrashCommandWithArguments (t * testing.T ) { 171fin := testdir .CreateTestDir () 172defer fin () 173 174target := t .TempDir () 175subdir ,file := createTestTree () 176 177// $1 is the argument given in the command, $2 the appended item path. 178command := writeScript (t ,`mv -f "$2" "$1"` )+ " " + target 179 180err := TrashCommand (command )(subdir ,file ) 181require .NoError (t ,err ) 182 183assert .FileExists (t ,filepath .Join (target ,"file2" )) 184assert .NoFileExists (t ,"test_dir/nested/file2" ) 185assert .Equal (t ,0 ,len (subdir .Files )) 186} 187 188// TestTrashCommandWithMv covers the mv recipe from the documentation. mv takes 189// its destination last, so the command places the item path itself instead of 190// having it appended. It also checks that the command is evaluated by a shell, 191// so a trash dir given in the config file can use ~. 192func TestTrashCommandWithMv (t * testing.T ) { 193fin := testdir .CreateTestDir () 194defer fin () 195 196home := t .TempDir () 197t .Setenv ("HOME" ,home ) 198require .NoError (t ,os .Mkdir (filepath .Join (home ,"mytrash" ),0o700 )) 199 200subdir ,file := createTestTree () 201 202err := TrashCommand (`mv -f "$1" ~/mytrash/` )(subdir ,file ) 203require .NoError (t ,err ) 204 205assert .FileExists (t ,filepath .Join (home ,"mytrash" ,"file2" )) 206assert .NoFileExists (t ,"test_dir/nested/file2" ) 207assert .Equal (t ,0 ,len (subdir .Files )) 208} 209 210// TestTrashCommandWithMvToMissingDir makes sure the trailing slash in the 211// documented mv recipe turns a missing trash dir into an error instead of 212// renaming the item onto the destination path. 213func TestTrashCommandWithMvToMissingDir (t * testing.T ) { 214fin := testdir .CreateTestDir () 215defer fin () 216 217home := t .TempDir () 218t .Setenv ("HOME" ,home ) 219 220subdir ,file := createTestTree () 221 222err := TrashCommand (`mv -f "$1" ~/mytrash/` )(subdir ,file ) 223require .Error (t ,err ) 224 225assert .NoFileExists (t ,filepath .Join (home ,"mytrash" )) 226assert .FileExists (t ,"test_dir/nested/file2" ) 227assert .Equal (t ,1 ,len (subdir .Files )) 228} 229 230func TestTrashCommandPassesPathAsArgumentAndEnvVar (t * testing.T ) { 231fin := testdir .CreateTestDir () 232defer fin () 233 234out := filepath .Join (t .TempDir (),"args" ) 235subdir ,file := createTestTree () 236 237command := writeScript (t ,`printf '%s\n' "$#" "$1" "$GDU_TRASH_PATH" > "` + out + `"` ) 238 239err := TrashCommand (command )(subdir ,file ) 240require .NoError (t ,err ) 241 242wantPath ,err := filepath .Abs ("test_dir/nested/file2" ) 243require .NoError (t ,err ) 244 245data ,err := os .ReadFile (out ) 246require .NoError (t ,err ) 247assert .Equal (t , []string {"1" ,wantPath ,wantPath },strings .Fields (string (data ))) 248} 249 250// TestTrashCommandWithSpecialCharsInName makes sure item names are never 251// interpreted as shell syntax. 252func TestTrashCommandWithSpecialCharsInName (t * testing.T ) { 253base := t .TempDir () 254name := "; touch pwned; echo 'x'" 255path := filepath .Join (base ,name ) 256require .NoError (t ,os .WriteFile (path , []byte ("x" ),0o600 )) 257 258dir := & analyze.Dir { 259File :& analyze.File {Name :filepath .Base (base )}, 260ItemCount :2 , 261BasePath :filepath .Dir (base ), 262 } 263file := & analyze.File {Name :name ,Size :1 ,Usage :1 ,Parent :dir } 264dir .Files = fs.Files {file } 265 266err := TrashCommand ("rm -f" )(dir ,file ) 267require .NoError (t ,err ) 268 269assert .NoFileExists (t ,path ) 270assert .NoFileExists (t ,filepath .Join (base ,"pwned" )) 271assert .NoFileExists (t ,"pwned" ) 272assert .Equal (t ,0 ,len (dir .Files )) 273} 274 275func TestTrashCommandFailing (t * testing.T ) { 276fin := testdir .CreateTestDir () 277defer fin () 278 279subdir ,file := createTestTree () 280 281err := TrashCommand (writeScript (t ,"exit 3" ))(subdir ,file ) 282require .Error (t ,err ) 283assert .Contains (t ,err .Error (),"running trash command" ) 284assert .Contains (t ,err .Error (),"exit status 3" ) 285 286assert .FileExists (t ,"test_dir/nested/file2" ) 287assert .Equal (t ,1 ,len (subdir .Files )) 288} 289 290func TestTrashCommandFailingWithStderr (t * testing.T ) { 291fin := testdir .CreateTestDir () 292defer fin () 293 294subdir ,file := createTestTree () 295 296err := TrashCommand (writeScript (t ,"echo 'trash dir is missing' >&2\nexit 1" ))(subdir ,file ) 297require .Error (t ,err ) 298assert .Contains (t ,err .Error (),"trash dir is missing" ) 299assert .Equal (t ,1 ,len (subdir .Files )) 300} 301 302// TestTrashCommandLeavingItemInPlace covers a command which succeeds without 303// removing the item, e.g. an interactive command answered with "no". 304func TestTrashCommandLeavingItemInPlace (t * testing.T ) { 305fin := testdir .CreateTestDir () 306defer fin () 307 308subdir ,file := createTestTree () 309 310err := TrashCommand ("true" )(subdir ,file ) 311require .NoError (t ,err ) 312 313assert .FileExists (t ,"test_dir/nested/file2" ) 314assert .Equal (t ,1 ,len (subdir .Files )) 315assert .Equal (t ,int64 (2 ),subdir .ItemCount ) 316} 317 318func TestTrashCommandAbsError (t * testing.T ) { 319sentinel := errors .New ("abs failed" ) 320mockTrashCommandOS (t ,func (ops * trashCommandOSOps ) { 321ops .abs = func (string ) (string ,error ) {return "" ,sentinel } 322 }) 323 324subdir ,file := createTestTree () 325 326err := TrashCommand ("rm -rf" )(subdir ,file ) 327require .ErrorIs (t ,err ,sentinel ) 328assert .Equal (t ,1 ,len (subdir .Files )) 329} 330 331func TestTrashCommandStderrTruncated (t * testing.T ) { 332mockTrashCommandOS (t ,func (ops * trashCommandOSOps ) { 333ops .run = func (_ string ,_ ,_ []string ,stderr io.Writer )error { 334_ ,err := stderr .Write ([]byte (strings .Repeat ("x" ,maxStderrLen + 10 ))) 335require .NoError (t ,err ) 336return errors .New ("failed" ) 337 } 338 }) 339 340subdir ,file := createTestTree () 341 342err := TrashCommand ("rm -rf" )(subdir ,file ) 343require .Error (t ,err ) 344assert .True (t ,strings .HasSuffix (err .Error (),"..." )) 345assert .Less (t ,len (err .Error ()),maxStderrLen + 100 ) 346}