github.com/anishathalye/periscope@v0.3.5/internal/periscope/info_test.go (about)

     1  package periscope
     2  
     3  import (
     4  	"github.com/anishathalye/periscope/internal/testfs"
     5  
     6  	"os"
     7  	"path/filepath"
     8  	"regexp"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/spf13/afero"
    13  )
    14  
    15  func TestInfoBasic(t *testing.T) {
    16  	fs := testfs.Read(`
    17  /a [10000 1]
    18  /b [10000 1]
    19  /c [10000 1]
    20  	`).Mkfs()
    21  	ps, out, _ := newTest(fs)
    22  	err := ps.Scan([]string{"/"}, &ScanOptions{})
    23  	check(t, err)
    24  	err = ps.Info([]string{"/b"}, &InfoOptions{})
    25  	check(t, err)
    26  	got := strings.TrimSpace(out.String())
    27  	expected := regexp.MustCompile(strings.TrimSpace(`
    28  ^/b
    29    short hash: ................
    30     full hash: ................................................................
    31    duplicates: 2
    32      /a
    33      /c$
    34  	`))
    35  	if !expected.MatchString(got) {
    36  		t.Fatalf("expected '%s', got '%s'", expected, got)
    37  	}
    38  }
    39  
    40  func TestInfoMultiple(t *testing.T) {
    41  	fs := testfs.Read(`
    42  /a [10000 1]
    43  /b [10000 1]
    44  /c [10000 1]
    45  	`).Mkfs()
    46  	ps, out, _ := newTest(fs)
    47  	err := ps.Scan([]string{"/"}, &ScanOptions{})
    48  	check(t, err)
    49  	err = ps.Info([]string{"/b", "/a"}, &InfoOptions{})
    50  	check(t, err)
    51  	got := strings.TrimSpace(out.String())
    52  	expected := regexp.MustCompile(strings.TrimSpace(`
    53  ^/b
    54    short hash: ................
    55     full hash: ................................................................
    56    duplicates: 2
    57      /a
    58      /c
    59  
    60  /a
    61    short hash: ................
    62     full hash: ................................................................
    63    duplicates: 2
    64      /b
    65      /c$
    66  	`))
    67  	if !expected.MatchString(got) {
    68  		t.Fatalf("expected '%s', got '%s'", expected, got)
    69  	}
    70  }
    71  
    72  func TestInfoRelative(t *testing.T) {
    73  	fs := testfs.Read(`
    74  /long/directory/path/a [10000 1]
    75  /long/directory/path/b [10000 1]
    76  /long/directory/other/a [10000 1]
    77  	`).Mkfs()
    78  	ps, out, _ := newTest(fs)
    79  	err := ps.Scan([]string{"/long/directory"}, &ScanOptions{})
    80  	check(t, err)
    81  	err = ps.Info([]string{"/long/directory/path/a"}, &InfoOptions{Relative: true})
    82  	check(t, err)
    83  	got := strings.TrimSpace(out.String())
    84  	expected := regexp.MustCompile(strings.TrimSpace(`
    85  ^/long/directory/path/a
    86    short hash: ................
    87     full hash: ................................................................
    88    duplicates: 2
    89      \.\./other/a
    90      b$
    91  	`))
    92  	if !expected.MatchString(got) {
    93  		t.Fatalf("expected '%s', got '%s'", expected, got)
    94  	}
    95  }
    96  
    97  func TestInfoRelativeTooLong(t *testing.T) {
    98  	fs := testfs.Read(`
    99  /long/directory/path/a [10000 1]
   100  /long/directory/path/b [10000 1]
   101  /s/a [10000 1]
   102  	`).Mkfs()
   103  	ps, out, _ := newTest(fs)
   104  	err := ps.Scan([]string{"/"}, &ScanOptions{})
   105  	check(t, err)
   106  	err = ps.Info([]string{"/long/directory/path/a"}, &InfoOptions{Relative: true})
   107  	check(t, err)
   108  	got := strings.TrimSpace(out.String())
   109  	expected := regexp.MustCompile(strings.TrimSpace(`
   110  ^/long/directory/path/a
   111    short hash: ................
   112     full hash: ................................................................
   113    duplicates: 2
   114      b
   115      /s/a$
   116  	`))
   117  	if !expected.MatchString(got) {
   118  		t.Fatalf("expected '%s', got '%s'", expected, got)
   119  	}
   120  }
   121  
   122  func TestInfoDirectory(t *testing.T) {
   123  	fs := testfs.Read(`
   124  /long/directory/path/a [10000 1]
   125  /s/a [10000 1]
   126  	`).Mkfs()
   127  	ps, _, _ := newTest(fs)
   128  	err := ps.Scan([]string{"/"}, &ScanOptions{})
   129  	check(t, err)
   130  	err = ps.Info([]string{"/long/directory/path"}, &InfoOptions{Relative: true})
   131  	checkErr(t, err)
   132  }
   133  
   134  func TestInfoBadSymlinks(t *testing.T) {
   135  	fs := afero.NewOsFs()
   136  	dir := tempDir()
   137  	defer os.RemoveAll(dir)
   138  	os.Symlink(filepath.Join(dir, "a"), filepath.Join(dir, "b"))
   139  	os.Symlink(filepath.Join(dir, "b"), filepath.Join(dir, "a"))
   140  	ps, _, _ := newTest(fs)
   141  	err := ps.Info([]string{filepath.Join(dir, "a")}, &InfoOptions{})
   142  	checkErr(t, err)
   143  }