github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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  	"path/filepath"
    10  	"testing"
    11  )
    12  
    13  func Test_file_outputPath(t *testing.T) {
    14  	type fields struct {
    15  		Path    string
    16  		Options *Options
    17  	}
    18  	tests := []struct {
    19  		name   string
    20  		fields fields
    21  		want   string
    22  	}{
    23  		{
    24  			name:   "Stdout",
    25  			fields: fields{Path: "/dev/stdout", Options: &Options{Stdout: true}},
    26  			want:   "/dev/stdout",
    27  		},
    28  		{
    29  			name:   "Test",
    30  			fields: fields{Path: "/dev/null", Options: &Options{Test: true}},
    31  			want:   "/dev/null",
    32  		},
    33  		{
    34  			name:   "Compress",
    35  			fields: fields{Path: "/tmp/test", Options: &Options{Suffix: ".gz"}},
    36  			want:   "/tmp/test.gz",
    37  		},
    38  		{
    39  			name:   "Decompress",
    40  			fields: fields{Path: "/tmp/test.gz", Options: &Options{Decompress: true, Suffix: ".gz"}},
    41  			want:   "/tmp/test",
    42  		},
    43  	}
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			f := &File{
    47  				Path:    tt.fields.Path,
    48  				Options: tt.fields.Options,
    49  			}
    50  			if got := f.outputPath(); got != tt.want {
    51  				t.Errorf("file.outputPath() = %v, want %v", got, tt.want)
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestFile_CheckPath(t *testing.T) {
    58  	type fields struct {
    59  		Path    string
    60  		Options *Options
    61  	}
    62  	tests := []struct {
    63  		name    string
    64  		fields  fields
    65  		wantErr bool
    66  	}{
    67  		{
    68  			name: "skip decompressing already does not have suffix",
    69  			fields: fields{
    70  				Path: "file",
    71  				Options: &Options{
    72  					Decompress: true,
    73  					Suffix:     ".gz",
    74  				},
    75  			},
    76  			wantErr: true,
    77  		},
    78  		{
    79  			name: "skip compressing already has suffix",
    80  			fields: fields{
    81  				Path: "file.gz",
    82  				Options: &Options{
    83  					Decompress: false,
    84  					Suffix:     ".gz",
    85  				},
    86  			},
    87  			wantErr: true,
    88  		},
    89  	}
    90  
    91  	tempDir := t.TempDir()
    92  
    93  	for _, tt := range tests {
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			path, err := os.Create(filepath.Join(tempDir, tt.fields.Path))
    96  			if err != nil {
    97  				t.Fatalf("File.CheckPath() error can't create temp file: %v", err)
    98  			}
    99  			defer path.Close()
   100  
   101  			f := &File{
   102  				Path:    path.Name(),
   103  				Options: tt.fields.Options,
   104  			}
   105  			if err := f.CheckPath(); (err != nil) != tt.wantErr {
   106  				t.Errorf("File.CheckPath() error = %v, wantErr %v", err, tt.wantErr)
   107  			}
   108  		})
   109  	}
   110  }
   111  
   112  func TestFile_CheckOutputPath(t *testing.T) {
   113  	type fields struct {
   114  		Path    string
   115  		Options *Options
   116  	}
   117  	tests := []struct {
   118  		name    string
   119  		fields  fields
   120  		wantErr bool
   121  	}{
   122  		{
   123  			name: "don't check output path if Stdout is true",
   124  			fields: fields{
   125  				Options: &Options{
   126  					Stdout: true,
   127  				},
   128  			},
   129  			wantErr: false,
   130  		},
   131  	}
   132  	for _, tt := range tests {
   133  		t.Run(tt.name, func(t *testing.T) {
   134  			f := &File{
   135  				Path:    tt.fields.Path,
   136  				Options: tt.fields.Options,
   137  			}
   138  			if err := f.CheckOutputPath(); (err != nil) != tt.wantErr {
   139  				t.Errorf("File.CheckOutputPath() error = %v, wantErr %v", err, tt.wantErr)
   140  			}
   141  		})
   142  	}
   143  }
   144  
   145  func TestFile_CheckOutputStdout(t *testing.T) {
   146  	type fields struct {
   147  		Path    string
   148  		Options *Options
   149  	}
   150  	tests := []struct {
   151  		name    string
   152  		fields  fields
   153  		wantErr bool
   154  	}{
   155  		{
   156  			name: "Stdout compress to device",
   157  			fields: fields{
   158  				Path:    "/dev/null",
   159  				Options: &Options{Stdout: true, Decompress: false, Force: false},
   160  			},
   161  			wantErr: true,
   162  		},
   163  		{
   164  			name: "Stdout compress to device force",
   165  			fields: fields{
   166  				Path:    "/dev/null",
   167  				Options: &Options{Stdout: true, Decompress: false, Force: true},
   168  			},
   169  			wantErr: false,
   170  		},
   171  		{
   172  			name: "Stdout compress redirect to file",
   173  			fields: fields{
   174  				Path:    "/tmp/test",
   175  				Options: &Options{Stdout: true, Decompress: false, Force: false},
   176  			},
   177  			wantErr: false,
   178  		},
   179  	}
   180  
   181  	for _, tt := range tests {
   182  		t.Run(tt.name, func(t *testing.T) {
   183  			f := &File{
   184  				Path:    tt.fields.Path,
   185  				Options: tt.fields.Options,
   186  			}
   187  			oldStdout := os.Stdout
   188  			var stdout *os.File
   189  			if f.Path[0:4] == "/dev" {
   190  				stdout, _ = os.Open(f.Path)
   191  			} else {
   192  				stdout, _ = os.Create(f.Path)
   193  				defer os.Remove(f.Path)
   194  			}
   195  			defer stdout.Close()
   196  
   197  			os.Stdout = stdout
   198  			if err := f.CheckOutputStdout(); (err != nil) != tt.wantErr {
   199  				t.Errorf("File.checkOutStdout() error = %v, wantErr %v", err, tt.wantErr)
   200  			}
   201  			os.Stdout = oldStdout
   202  		})
   203  	}
   204  }
   205  
   206  func TestFile_Cleanup(t *testing.T) {
   207  	type fields struct {
   208  		Path    string
   209  		Options *Options
   210  	}
   211  	tests := []struct {
   212  		name    string
   213  		fields  fields
   214  		exists  bool
   215  		wantErr bool
   216  	}{
   217  		{
   218  			name: "file should be deleted",
   219  			fields: fields{
   220  				Options: &Options{},
   221  			},
   222  			exists:  false,
   223  			wantErr: false,
   224  		},
   225  		{
   226  			name: "file should not be deleted Keep is true",
   227  			fields: fields{
   228  				Options: &Options{Keep: true},
   229  			},
   230  			exists:  true,
   231  			wantErr: false,
   232  		},
   233  		{
   234  			name: "file should not be deleted Stdout is true",
   235  			fields: fields{
   236  				Options: &Options{Stdout: true},
   237  			},
   238  			exists:  true,
   239  			wantErr: false,
   240  		},
   241  		{
   242  			name: "file should not be deleted Test is true",
   243  			fields: fields{
   244  				Options: &Options{Stdout: true},
   245  			},
   246  			exists:  true,
   247  			wantErr: false,
   248  		},
   249  	}
   250  
   251  	tempDir := t.TempDir()
   252  
   253  	for _, tt := range tests {
   254  		t.Run(tt.name, func(t *testing.T) {
   255  			path, err := os.CreateTemp(tempDir, "cleanup-test")
   256  			if err != nil {
   257  				t.Errorf("File.Cleanup() error can't create temp file: %v", err)
   258  			}
   259  			defer path.Close()
   260  			f := &File{
   261  				Path:    path.Name(),
   262  				Options: tt.fields.Options,
   263  			}
   264  			if err := f.Cleanup(); (err != nil) != tt.wantErr {
   265  				t.Errorf("File.Cleanup() error = %v, wantErr %v", err, tt.wantErr)
   266  			}
   267  			_, err = os.Stat(f.Path)
   268  			if tt.exists && err != nil {
   269  				t.Errorf("File.Cleanup() file should be deleted")
   270  			}
   271  			if !tt.exists && err == nil {
   272  				t.Errorf("File.Cleanup() file should stay")
   273  			}
   274  		})
   275  	}
   276  }
   277  
   278  func TestFile_Process(t *testing.T) {
   279  	tempDir := t.TempDir()
   280  	path, err := os.CreateTemp(tempDir, "process-test")
   281  	if err != nil {
   282  		t.Fatalf("File.Process() error can't create temp file: %v", err)
   283  	}
   284  	defer path.Close()
   285  
   286  	f := File{
   287  		Path: path.Name(),
   288  		Options: &Options{
   289  			Decompress: false,
   290  			Blocksize:  128,
   291  			Level:      -1,
   292  			Processes:  1,
   293  			Suffix:     ".gz",
   294  		},
   295  	}
   296  
   297  	if err := f.Process(); err != nil {
   298  		t.Errorf("File.Process() compression error = %v", err)
   299  	}
   300  
   301  	f.Path = f.Path + f.Options.Suffix
   302  	f.Options.Decompress = true
   303  	if err := f.Process(); err != nil {
   304  		t.Errorf("File.Process() decompression error = %v", err)
   305  	}
   306  }