github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/mv/mv_test.go (about)

     1  // Copyright 2015-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 main
     6  
     7  import (
     8  	"bytes"
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/u-root/u-root/pkg/testutil"
    16  )
    17  
    18  type makeit struct {
    19  	n string      // name
    20  	m os.FileMode // mode
    21  	c []byte      // content
    22  }
    23  
    24  var old = makeit{
    25  	n: "old.txt",
    26  	m: 0777,
    27  	c: []byte("old"),
    28  }
    29  
    30  var new = makeit{
    31  	n: "new.txt",
    32  	m: 0777,
    33  	c: []byte("new"),
    34  }
    35  
    36  var tests = []makeit{
    37  	{
    38  		n: "hi1.txt",
    39  		m: 0666,
    40  		c: []byte("hi"),
    41  	},
    42  	{
    43  		n: "hi2.txt",
    44  		m: 0777,
    45  		c: []byte("hi"),
    46  	},
    47  	old,
    48  	new,
    49  }
    50  
    51  func setup() (string, error) {
    52  	d, err := ioutil.TempDir(os.TempDir(), "hi.dir")
    53  	if err != nil {
    54  		return "", err
    55  	}
    56  
    57  	tmpdir := filepath.Join(d, "hi.sub.dir")
    58  	if err := os.Mkdir(tmpdir, 0777); err != nil {
    59  		return "", err
    60  	}
    61  
    62  	for _, t := range tests {
    63  		if err := ioutil.WriteFile(filepath.Join(d, t.n), []byte(t.c), t.m); err != nil {
    64  			return "", err
    65  		}
    66  	}
    67  
    68  	return d, nil
    69  }
    70  
    71  func TestMv(t *testing.T) {
    72  	d, err := setup()
    73  	if err != nil {
    74  		t.Fatal("err")
    75  	}
    76  	defer os.RemoveAll(d)
    77  
    78  	t.Logf("Renaming file...")
    79  	{
    80  		files := []string{filepath.Join(d, "hi1.txt"), filepath.Join(d, "hi4.txt")}
    81  		res := testutil.Command(t, files...)
    82  		_, err = res.CombinedOutput()
    83  		if err = testutil.IsExitCode(err, 0); err != nil {
    84  			t.Error(err)
    85  		}
    86  	}
    87  
    88  	dsub := filepath.Join(d, "hi.sub.dir")
    89  
    90  	t.Logf("Moving files to directory...")
    91  	{
    92  		files := []string{filepath.Join(d, "hi2.txt"), filepath.Join(d, "hi4.txt"), dsub}
    93  		res := testutil.Command(t, files...)
    94  		_, err = res.CombinedOutput()
    95  		if err = testutil.IsExitCode(err, 0); err != nil {
    96  			t.Error(err)
    97  		}
    98  	}
    99  }
   100  
   101  func TestMvUpdate(t *testing.T) {
   102  	*update = true
   103  	d, err := setup()
   104  	if err != nil {
   105  		t.Error(err)
   106  	}
   107  	defer os.RemoveAll(d)
   108  	t.Logf("Testing mv -u...")
   109  
   110  	// Ensure that the newer file actually has a newer timestamp
   111  	currentTime := time.Now().Local()
   112  	oldTime := currentTime.Add(-10 * time.Second)
   113  	err = os.Chtimes(filepath.Join(d, old.n), oldTime, oldTime)
   114  	if err != nil {
   115  		t.Error(err)
   116  	}
   117  	err = os.Chtimes(filepath.Join(d, new.n), currentTime, currentTime)
   118  	if err != nil {
   119  		t.Error(err)
   120  	}
   121  
   122  	// Check that it doesn't downgrade files with -u switch
   123  	{
   124  		files := []string{"-u", filepath.Join(d, old.n), filepath.Join(d, new.n)}
   125  		res := testutil.Command(t, files...)
   126  		_, err = res.CombinedOutput()
   127  		if err = testutil.IsExitCode(err, 0); err != nil {
   128  			t.Error(err)
   129  		}
   130  		newContent, err := ioutil.ReadFile(filepath.Join(d, new.n))
   131  		if err != nil {
   132  			t.Error(err)
   133  		}
   134  		if bytes.Equal(newContent, old.c) {
   135  			t.Error("Newer file was overwritten by older file. Should not happen with -u.")
   136  		}
   137  	}
   138  
   139  	// Check that it does update files with -u switch
   140  	{
   141  		files := []string{"-u", filepath.Join(d, new.n), filepath.Join(d, old.n)}
   142  		res := testutil.Command(t, files...)
   143  		_, err = res.CombinedOutput()
   144  		if err = testutil.IsExitCode(err, 0); err != nil {
   145  			t.Error(err)
   146  		}
   147  		newContent, err := ioutil.ReadFile(filepath.Join(d, old.n))
   148  		if err != nil {
   149  			t.Error(err)
   150  		}
   151  		if !bytes.Equal(newContent, new.c) {
   152  			t.Error("Older file was not overwritten by newer file. Should happen with -u.")
   153  		}
   154  		if _, err := os.Lstat(filepath.Join(d, old.n)); err != nil {
   155  			t.Error("The new file shouldn't be there anymore.")
   156  		}
   157  	}
   158  }
   159  
   160  func TestMain(m *testing.M) {
   161  	testutil.Run(m, main)
   162  }