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

     1  package periscope
     2  
     3  import (
     4  	"github.com/anishathalye/periscope/internal/testfs"
     5  
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/spf13/afero"
    13  )
    14  
    15  func TestReportBasic(t *testing.T) {
    16  	fs := testfs.Read(`
    17  /d1/a [10000 1]
    18  /d1/b [13923446 2]
    19  /d1/c [1191 3]
    20  /d2/a [10000 1]
    21  /d2/b [13923446 2]
    22  /d2/c [1002 5]
    23  /d3/a [10000 1]
    24  /.x [123 5]
    25  /.y [123 5]
    26  	`).Mkfs()
    27  	ps, out, _ := newTest(fs)
    28  	ps.Scan([]string{"/"}, &ScanOptions{})
    29  	err := ps.Report("", &ReportOptions{})
    30  	check(t, err)
    31  	got := strings.TrimSpace(out.String())
    32  	expected := strings.TrimSpace(`
    33  14 MB
    34    /d1/b
    35    /d2/b
    36  
    37  10 kB
    38    /d1/a
    39    /d2/a
    40    /d3/a
    41  
    42  123 B
    43    /.x
    44    /.y
    45  	`)
    46  	if got != expected {
    47  		t.Fatalf("expected '%s', got '%s'", expected, got)
    48  	}
    49  }
    50  
    51  func TestReportSubdirectory(t *testing.T) {
    52  	fs := testfs.Read(`
    53  /d1/a [10000 1]
    54  /d1/b [13923446 2]
    55  /d1/c [1191 3]
    56  /d2/a [10000 1]
    57  /d2/b [13923446 2]
    58  /d2/c [1002 5]
    59  /d3/a [10000 1]
    60  /.x [123 5]
    61  /.y [123 5]
    62  	`).Mkfs()
    63  	ps, out, _ := newTest(fs)
    64  	ps.Scan([]string{"/"}, &ScanOptions{})
    65  	err := ps.Report("/d3", &ReportOptions{})
    66  	check(t, err)
    67  	got := strings.TrimSpace(out.String())
    68  	expected := strings.TrimSpace(`
    69  10 kB
    70    /d1/a
    71    /d2/a
    72    /d3/a
    73  	`)
    74  	if got != expected {
    75  		t.Fatalf("expected '%s', got '%s'", expected, got)
    76  	}
    77  }
    78  
    79  func TestReportEmpty(t *testing.T) {
    80  	fs := testfs.New(nil).Mkfs()
    81  	ps, out, _ := newTest(fs)
    82  	err := ps.Report("", &ReportOptions{})
    83  	check(t, err)
    84  	got := strings.TrimSpace(out.String())
    85  	if got != "" {
    86  		t.Fatalf("expected no output, got '%s'", got)
    87  	}
    88  }
    89  
    90  func TestReportRelative(t *testing.T) {
    91  	fs := afero.NewOsFs()
    92  	dir := tempDir()
    93  	defer os.RemoveAll(dir)
    94  	ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a', 'a'}, 0o644)
    95  	ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a', 'a'}, 0o644)
    96  	os.Mkdir(filepath.Join(dir, "d"), 0o755)
    97  	ioutil.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
    98  	ioutil.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
    99  	ps, out, _ := newTest(fs)
   100  	oldWd, err := os.Getwd()
   101  	if err != nil {
   102  		panic(err)
   103  	}
   104  	os.Chdir(dir)
   105  	defer os.Chdir(oldWd)
   106  	ps.Scan([]string{"."}, &ScanOptions{})
   107  	err = ps.Report("", &ReportOptions{Relative: true})
   108  	check(t, err)
   109  	got := strings.TrimSpace(out.String())
   110  	expected := strings.TrimSpace(`
   111  2 B
   112    x
   113    y
   114  
   115  1 B
   116    d/a
   117    d/b
   118  	`)
   119  	if got != expected {
   120  		t.Fatalf("expected '%s', got '%s'", expected, got)
   121  	}
   122  }
   123  
   124  func TestReportRelativeArgument(t *testing.T) {
   125  	fs := afero.NewOsFs()
   126  	dir := tempDir()
   127  	defer os.RemoveAll(dir)
   128  	ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a', 'a'}, 0o644)
   129  	ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a', 'a'}, 0o644)
   130  	os.Mkdir(filepath.Join(dir, "d"), 0o755)
   131  	ioutil.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
   132  	ioutil.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
   133  	ps, out, _ := newTest(fs)
   134  	ps.Scan([]string{dir}, &ScanOptions{})
   135  	err := ps.Report(filepath.Join(dir, "d"), &ReportOptions{Relative: true})
   136  	check(t, err)
   137  	got := strings.TrimSpace(out.String())
   138  	expected := strings.TrimSpace(`
   139  1 B
   140    a
   141    b
   142  	`)
   143  	if got != expected {
   144  		t.Fatalf("expected '%s', got '%s'", expected, got)
   145  	}
   146  }