fork/gdu
git clone https://git.julia.blue/fork/gdu
011b7e8
master
1package tui 2 3import ( 4"bytes" 5"errors" 6"os/exec" 7"runtime" 8"testing" 9 10"github.com/stretchr/testify/assert" 11 12"github.com/dundee/gdu/v5/internal/testapp" 13) 14 15func lookPathFor (names ... string )func (string ) (string ,error ) { 16available := make (map [string ]bool ,len (names )) 17for _ ,name := range names { 18available [name ]= true 19 } 20return func (name string ) (string ,error ) { 21if available [name ] { 22return "/usr/bin/" + name ,nil 23 } 24return "" ,errors .New ("executable not found" ) 25 } 26} 27 28func TestClipboardCommand (t * testing.T ) { 29for name ,tt := range map [string ]struct { 30env clipboardEnv 31want []string 32wantOK bool 33 }{ 34"darwin uses pbcopy" : { 35env :clipboardEnv {goos :osDarwin ,lookPath :lookPathFor ("pbcopy" )}, 36want : []string {"/usr/bin/pbcopy" },wantOK :true , 37 }, 38"windows uses clip" : { 39env :clipboardEnv {goos :osWindows ,lookPath :lookPathFor ("clip" )}, 40want : []string {"/usr/bin/clip" },wantOK :true , 41 }, 42"linux uses xclip" : { 43env :clipboardEnv {goos :"linux" ,lookPath :lookPathFor ("xclip" ,"xsel" )}, 44want : []string {"/usr/bin/xclip" ,"-selection" ,"clipboard" },wantOK :true , 45 }, 46"linux falls back to xsel" : { 47env :clipboardEnv {goos :"linux" ,lookPath :lookPathFor ("xsel" )}, 48want : []string {"/usr/bin/xsel" ,"--clipboard" ,"--input" },wantOK :true , 49 }, 50"freebsd uses the same tools as linux" : { 51env :clipboardEnv {goos :"freebsd" ,lookPath :lookPathFor ("xclip" )}, 52want : []string {"/usr/bin/xclip" ,"-selection" ,"clipboard" },wantOK :true , 53 }, 54"wayland session prefers wl-copy" : { 55env :clipboardEnv {goos :"linux" ,wayland :true ,lookPath :lookPathFor ("wl-copy" ,"xclip" )}, 56want : []string {"/usr/bin/wl-copy" },wantOK :true , 57 }, 58"without wayland session prefers xclip over wl-copy" : { 59env :clipboardEnv {goos :"linux" ,wayland :false ,lookPath :lookPathFor ("wl-copy" ,"xclip" )}, 60want : []string {"/usr/bin/xclip" ,"-selection" ,"clipboard" },wantOK :true , 61 }, 62"falls back to wl-copy when only tool" : { 63env :clipboardEnv {goos :"linux" ,wayland :false ,lookPath :lookPathFor ("wl-copy" )}, 64want : []string {"/usr/bin/wl-copy" },wantOK :true , 65 }, 66"no tool available" : { 67env :clipboardEnv {goos :"linux" ,lookPath :lookPathFor ()}, 68want :nil ,wantOK :false , 69 }, 70 } { 71t .Run (name ,func (t * testing.T ) { 72got ,ok := tt .env .command () 73assert .Equal (t ,tt .wantOK ,ok ) 74assert .Equal (t ,tt .want ,got ) 75 }) 76 } 77} 78 79func TestCopyToClipboardRoundTrip (t * testing.T ) { 80if runtime .GOOS != osDarwin { 81t .Skip ("clipboard round-trip is only verified with pbcopy/pbpaste on darwin" ) 82 } 83if _ ,err := exec .LookPath ("pbcopy" );err != nil { 84t .Skip ("pbcopy not available" ) 85 } 86 87// Preserve and restore whatever the user already had on the clipboard. 88before ,_ := exec .Command ("pbpaste" ).Output () 89t .Cleanup (func () { 90restore := exec .Command ("pbcopy" ) 91restore .Stdin = bytes .NewReader (before ) 92_ = restore .Run () 93 }) 94 95const path = "/tmp/gdu clipboard test/some file" 96if err := copyToClipboard (path );err != nil { 97t .Fatal (err ) 98 } 99 100out ,err := exec .Command ("pbpaste" ).Output () 101if err != nil { 102t .Fatal (err ) 103 } 104assert .Equal (t ,path ,string (out )) 105} 106 107func TestCopySelectedPathWithoutCurrentDir (t * testing.T ) { 108app ,simScreen := testapp .CreateTestAppWithSimScreen (50 ,50 ) 109defer simScreen .Fini () 110 111ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 112 113// No directory analyzed yet: copying must be a safe no-op. 114assert .NotPanics (t ,ui .copySelectedPath ) 115}