github.com/4thel00z/pcopy@v0.0.0-20230830212547-a1758a3a86bc/util/util_test.go (about)

     1  package util
     2  
     3  import (
     4  	"github.com/4thel00z/pcopy/test"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestExpandHome_WithTilde(t *testing.T) {
    11  	test.StrEquals(t, os.Getenv("HOME")+"/this/is/a/path", ExpandHome("~/this/is/a/path"))
    12  }
    13  
    14  func TestExpandHome_NoTilde(t *testing.T) {
    15  	test.StrEquals(t, "/this/is/an/absolute/path", ExpandHome("/this/is/an/absolute/path"))
    16  }
    17  
    18  func TestCollapseHome_HasHomePrefix(t *testing.T) {
    19  	test.StrEquals(t, "~/this/is/a/path", CollapseHome(os.Getenv("HOME")+"/this/is/a/path"))
    20  }
    21  
    22  func TestCollapseHome_NoHomePrefix(t *testing.T) {
    23  	test.StrEquals(t, "/this/is/an/absolute/path", CollapseHome("/this/is/an/absolute/path"))
    24  }
    25  
    26  func TestBytesToHuman_Small(t *testing.T) {
    27  	test.StrEquals(t, "10 B", BytesToHuman(10))
    28  }
    29  
    30  func TestBytesToHuman_Large(t *testing.T) {
    31  	test.StrEquals(t, "10.1 MB", BytesToHuman(10590617))
    32  }
    33  
    34  func TestCommonPrefix_Empty(t *testing.T) {
    35  	var paths []string
    36  	test.StrEquals(t, "", commonPrefix(paths))
    37  }
    38  
    39  func TestCommonPrefix_1(t *testing.T) {
    40  	paths := []string{
    41  		"/home/phil/code/pcopy/go.mod",
    42  		"/home/phil/code/pcopy/go.sum",
    43  	}
    44  	test.StrEquals(t, "/home/phil/code/pcopy", commonPrefix(paths))
    45  }
    46  
    47  func TestCommonPrefix_2(t *testing.T) {
    48  	paths := []string{
    49  		"/home/phil/code/pcopy/go.mod",
    50  		"/home/phil/file.txt",
    51  	}
    52  	test.StrEquals(t, "/home/phil", commonPrefix(paths))
    53  }
    54  
    55  func TestCommonPrefix_NoCommonPrefix(t *testing.T) {
    56  	paths := []string{
    57  		"/home/phil/code/pcopy/go.mod",
    58  		"/etc/file.txt",
    59  	}
    60  	test.StrEquals(t, "", commonPrefix(paths))
    61  }
    62  
    63  func TestCommonPrefix_SingleFile(t *testing.T) {
    64  	paths := []string{
    65  		"/home/phil/code/pcopy",
    66  	}
    67  	test.StrEquals(t, "/home/phil/code/pcopy", commonPrefix(paths))
    68  }
    69  
    70  func TestRelativizePaths_Empty(t *testing.T) {
    71  	var files []string
    72  	baseDir, relativeFiles, err := relativizeFiles(files)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	test.StrEquals(t, "", baseDir)
    77  	test.Int64Equals(t, 0, int64(len(relativeFiles)))
    78  }
    79  
    80  func TestRelativizePaths_AbsFilesOnly(t *testing.T) {
    81  	files := []string{
    82  		"/home/phil/code/pcopy/go.mod",
    83  		"/home/phil/code/pcopy/go.sum",
    84  		"/home/phil/code/fsdup/main.go",
    85  	}
    86  	baseDir, relativeFiles, err := relativizeFiles(files)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	test.StrEquals(t, "/home/phil/code", baseDir)
    91  	test.StrEquals(t, "pcopy/go.mod", relativeFiles[0])
    92  	test.StrEquals(t, "pcopy/go.sum", relativeFiles[1])
    93  	test.StrEquals(t, "fsdup/main.go", relativeFiles[2])
    94  }
    95  
    96  func TestRelativizePaths_AbsFilesNoCommonPrefix(t *testing.T) {
    97  	files := []string{
    98  		"/home/phil/code/pcopy/go.mod",
    99  		"/etc/file.txt",
   100  	}
   101  	baseDir, relativeFiles, err := relativizeFiles(files)
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	test.StrEquals(t, "", baseDir)
   106  	test.StrEquals(t, "home/phil/code/pcopy/go.mod", relativeFiles[0])
   107  	test.StrEquals(t, "etc/file.txt", relativeFiles[1])
   108  }
   109  
   110  func TestRelativizePaths_OnlyRelFiles(t *testing.T) {
   111  	tmpDir := t.TempDir()
   112  	if err := os.Chdir(tmpDir); err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	files := []string{
   116  		"some/file.txt",
   117  		"other/file2.txt",
   118  		"file3.txt",
   119  	}
   120  	baseDir, relativeFiles, err := relativizeFiles(files)
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  	test.StrEquals(t, tmpDir, baseDir)
   125  	test.StrEquals(t, "some/file.txt", relativeFiles[0])
   126  	test.StrEquals(t, "other/file2.txt", relativeFiles[1])
   127  	test.StrEquals(t, "file3.txt", relativeFiles[2])
   128  }
   129  
   130  func TestRelativizePaths_RelAndAbsFiles(t *testing.T) {
   131  	tmpDir := t.TempDir()
   132  	if err := os.Chdir(tmpDir); err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	files := []string{
   136  		"some/file.txt",
   137  		"other/file2.txt",
   138  		"/etc/pcopy/server.conf",
   139  	}
   140  	baseDir, relativeFiles, err := relativizeFiles(files)
   141  	if err != nil {
   142  		t.Fatal(err)
   143  	}
   144  	test.StrEquals(t, "", baseDir)
   145  	test.StrEquals(t, tmpDir[1:]+"/some/file.txt", relativeFiles[0])
   146  	test.StrEquals(t, tmpDir[1:]+"/other/file2.txt", relativeFiles[1])
   147  	test.StrEquals(t, "etc/pcopy/server.conf", relativeFiles[2])
   148  }
   149  
   150  func TestRelativizePaths_SingleRelFile(t *testing.T) {
   151  	tmpDir := t.TempDir()
   152  	if err := os.Chdir(tmpDir); err != nil {
   153  		t.Fatal(err)
   154  	}
   155  	files := []string{
   156  		"dir/file.txt",
   157  	}
   158  	baseDir, relativeFiles, err := relativizeFiles(files)
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  	test.StrEquals(t, tmpDir+"/dir", baseDir)
   163  	test.StrEquals(t, "file.txt", relativeFiles[0])
   164  }
   165  
   166  func TestDurationToHuman_SevenDays(t *testing.T) {
   167  	d := 7 * 24 * time.Hour
   168  	test.StrEquals(t, "7d", DurationToHuman(d))
   169  }
   170  
   171  func TestDurationToHuman_MoreThanOneDay(t *testing.T) {
   172  	d := 49 * time.Hour
   173  	test.StrEquals(t, "2d1h", DurationToHuman(d))
   174  }
   175  
   176  func TestDurationToHuman_LessThanOneDay(t *testing.T) {
   177  	d := 17*time.Hour + 15*time.Minute
   178  	test.StrEquals(t, "17h15m", DurationToHuman(d))
   179  }
   180  
   181  func TestDurationToHuman_TenOfThings(t *testing.T) {
   182  	d := 10*time.Hour + 10*time.Minute + 10*time.Second
   183  	test.StrEquals(t, "10h10m10s", DurationToHuman(d))
   184  }
   185  
   186  func TestDurationToHuman_Zero(t *testing.T) {
   187  	test.StrEquals(t, "0", DurationToHuman(0))
   188  }
   189  
   190  func TestParseDuration_ZeroSuccess(t *testing.T) {
   191  	d, err := ParseDuration("0")
   192  	if err != nil {
   193  		t.Fatal(err)
   194  	}
   195  	if d != 0 {
   196  		t.Fatalf("expected %d, got %d", 0, d)
   197  	}
   198  }
   199  
   200  func TestParseDuration_SecondsOnly(t *testing.T) {
   201  	d, err := ParseDuration("3600")
   202  	if err != nil {
   203  		t.Fatal(err)
   204  	}
   205  	if d != time.Hour {
   206  		t.Fatalf("expected %d, got %d", time.Hour, d)
   207  	}
   208  }
   209  
   210  func TestParseDuration_WithDaysSuccess(t *testing.T) {
   211  	d, err := ParseDuration("10d")
   212  	if err != nil {
   213  		t.Fatal(err)
   214  	}
   215  	if d != 10*24*time.Hour {
   216  		t.Fatalf("expected %d, got %d", 10*24*time.Hour, d)
   217  	}
   218  }
   219  
   220  func TestParseDuration_WithoutDaysSuccess(t *testing.T) {
   221  	d, err := ParseDuration("10h5m")
   222  	if err != nil {
   223  		t.Fatal(err)
   224  	}
   225  	if d != 10*time.Hour+5*time.Minute {
   226  		t.Fatalf("expected %d, got %d", 10*time.Hour+5*time.Minute, d)
   227  	}
   228  }
   229  
   230  func TestParseDuration_WithDaysAndHoursFailure(t *testing.T) {
   231  	_, err := ParseDuration("10d1h") // not supported
   232  	if err == nil {
   233  		t.Fatalf("expected error, got none")
   234  	}
   235  }
   236  
   237  func TestParseDuration_WithWeeksSuccess(t *testing.T) {
   238  	d, err := ParseDuration("2w")
   239  	if err != nil {
   240  		t.Fatal(err)
   241  	}
   242  	if d != 2*7*24*time.Hour {
   243  		t.Fatalf("expected %d, got %d", 2*7*24*time.Hour, d)
   244  	}
   245  }
   246  
   247  func TestParseDuration_WithMonthsSuccess(t *testing.T) {
   248  	d, err := ParseDuration("2mo")
   249  	if err != nil {
   250  		t.Fatal(err)
   251  	}
   252  	if d != 2*30*24*time.Hour {
   253  		t.Fatalf("expected %d, got %d", 2*30*24*time.Hour, d)
   254  	}
   255  }
   256  
   257  func TestParseDuration_WithYearsSuccess(t *testing.T) {
   258  	d, err := ParseDuration("2y")
   259  	if err != nil {
   260  		t.Fatal(err)
   261  	}
   262  	if d != 2*365*24*time.Hour {
   263  		t.Fatalf("expected %d, got %d", 2*365*24*time.Hour, d)
   264  	}
   265  }
   266  
   267  func TestParseSize_10GSuccess(t *testing.T) {
   268  	s, err := ParseSize("10G")
   269  	if err != nil {
   270  		t.Fatal(err)
   271  	}
   272  	test.Int64Equals(t, 10*1024*1024*1024, s)
   273  }
   274  
   275  func TestParseSize_10MUpperCaseSuccess(t *testing.T) {
   276  	s, err := ParseSize("10M")
   277  	if err != nil {
   278  		t.Fatal(err)
   279  	}
   280  	test.Int64Equals(t, 10*1024*1024, s)
   281  }
   282  
   283  func TestParseSize_10kLowerCaseSuccess(t *testing.T) {
   284  	s, err := ParseSize("10k")
   285  	if err != nil {
   286  		t.Fatal(err)
   287  	}
   288  	test.Int64Equals(t, 10*1024, s)
   289  }
   290  
   291  func TestParseSize_FailureInvalid(t *testing.T) {
   292  	_, err := ParseSize("not a size")
   293  	if err == nil {
   294  		t.Fatalf("expected error, but got none")
   295  	}
   296  }