github.com/rck/u-root@v0.0.0-20180106144920-7eb602e381bb/pkg/gzip/file_test.go (about) 1 package gzip 2 3 import ( 4 "os" 5 "testing" 6 ) 7 8 func Test_file_outputPath(t *testing.T) { 9 type fields struct { 10 Path string 11 Options *Options 12 } 13 tests := []struct { 14 name string 15 fields fields 16 want string 17 }{ 18 { 19 name: "Stdout", 20 fields: fields{Path: "/dev/stdout", Options: &Options{Stdout: true}}, 21 want: "/dev/stdout", 22 }, 23 { 24 name: "Test", 25 fields: fields{Path: "/dev/null", Options: &Options{Test: true}}, 26 want: "/dev/null", 27 }, 28 { 29 name: "Compress", 30 fields: fields{Path: "/tmp/test", Options: &Options{Suffix: ".gz"}}, 31 want: "/tmp/test.gz", 32 }, 33 { 34 name: "Decompress", 35 fields: fields{Path: "/tmp/test.gz", Options: &Options{Decompress: true, Suffix: ".gz"}}, 36 want: "/tmp/test", 37 }, 38 } 39 for _, tt := range tests { 40 t.Run(tt.name, func(t *testing.T) { 41 f := &File{ 42 Path: tt.fields.Path, 43 Options: tt.fields.Options, 44 } 45 if got := f.outputPath(); got != tt.want { 46 t.Errorf("file.outputPath() = %v, want %v", got, tt.want) 47 } 48 }) 49 } 50 } 51 52 func TestFile_CheckPath(t *testing.T) { 53 type fields struct { 54 Path string 55 Options *Options 56 } 57 tests := []struct { 58 name string 59 fields fields 60 wantErr bool 61 }{ 62 // TODO: Add test cases. 63 } 64 for _, tt := range tests { 65 t.Run(tt.name, func(t *testing.T) { 66 f := &File{ 67 Path: tt.fields.Path, 68 Options: tt.fields.Options, 69 } 70 if err := f.CheckPath(); (err != nil) != tt.wantErr { 71 t.Errorf("File.CheckPath() error = %v, wantErr %v", err, tt.wantErr) 72 } 73 }) 74 } 75 } 76 77 func TestFile_CheckOutputPath(t *testing.T) { 78 type fields struct { 79 Path string 80 Options *Options 81 } 82 tests := []struct { 83 name string 84 fields fields 85 wantErr bool 86 }{ 87 // TODO: Add test cases. 88 } 89 for _, tt := range tests { 90 t.Run(tt.name, func(t *testing.T) { 91 f := &File{ 92 Path: tt.fields.Path, 93 Options: tt.fields.Options, 94 } 95 if err := f.CheckOutputPath(); (err != nil) != tt.wantErr { 96 t.Errorf("File.CheckOutputPath() error = %v, wantErr %v", err, tt.wantErr) 97 } 98 }) 99 } 100 } 101 func TestFile_CheckOutputStdout(t *testing.T) { 102 type fields struct { 103 Path string 104 Options *Options 105 } 106 tests := []struct { 107 name string 108 fields fields 109 wantErr bool 110 }{ 111 { 112 name: "Stdout compress to device", 113 fields: fields{ 114 Path: "/dev/null", 115 Options: &Options{Stdout: true, Decompress: false, Force: false}, 116 }, 117 wantErr: true, 118 }, 119 { 120 name: "Stdout compress to device force", 121 fields: fields{ 122 Path: "/dev/null", 123 Options: &Options{Stdout: true, Decompress: false, Force: true}, 124 }, 125 wantErr: false, 126 }, 127 { 128 name: "Stdout compress redirect to file", 129 fields: fields{ 130 Path: "/tmp/test", 131 Options: &Options{Stdout: true, Decompress: false, Force: false}, 132 }, 133 wantErr: false, 134 }, 135 } 136 137 for _, tt := range tests { 138 t.Run(tt.name, func(t *testing.T) { 139 f := &File{ 140 Path: tt.fields.Path, 141 Options: tt.fields.Options, 142 } 143 oldStdout := os.Stdout 144 var stdout *os.File 145 if f.Path[0:4] == "/dev" { 146 stdout, _ = os.Open(f.Path) 147 } else { 148 stdout, _ = os.Create(f.Path) 149 defer os.Remove(f.Path) 150 } 151 defer stdout.Close() 152 153 os.Stdout = stdout 154 if err := f.CheckOutputStdout(); (err != nil) != tt.wantErr { 155 t.Errorf("File.checkOutStdout() error = %v, wantErr %v", err, tt.wantErr) 156 } 157 os.Stdout = oldStdout 158 }) 159 } 160 } 161 162 func TestFile_Cleanup(t *testing.T) { 163 type fields struct { 164 Path string 165 Options *Options 166 } 167 tests := []struct { 168 name string 169 fields fields 170 wantErr bool 171 }{ 172 // TODO: Add test cases. 173 } 174 for _, tt := range tests { 175 t.Run(tt.name, func(t *testing.T) { 176 f := &File{ 177 Path: tt.fields.Path, 178 Options: tt.fields.Options, 179 } 180 if err := f.Cleanup(); (err != nil) != tt.wantErr { 181 t.Errorf("File.Cleanup() error = %v, wantErr %v", err, tt.wantErr) 182 } 183 }) 184 } 185 } 186 187 func TestFile_Process(t *testing.T) { 188 type fields struct { 189 Path string 190 Options *Options 191 } 192 tests := []struct { 193 name string 194 fields fields 195 wantErr bool 196 }{ 197 // TODO: Add test cases. 198 } 199 for _, tt := range tests { 200 t.Run(tt.name, func(t *testing.T) { 201 f := &File{ 202 Path: tt.fields.Path, 203 Options: tt.fields.Options, 204 } 205 if err := f.Process(); (err != nil) != tt.wantErr { 206 t.Errorf("File.Process() error = %v, wantErr %v", err, tt.wantErr) 207 } 208 }) 209 } 210 }