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