github.hscsec.cn/u-root/u-root@v7.0.0+incompatible/cmds/core/ls/ls_test.go (about)

     1  // Copyright 2017 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  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/u-root/u-root/pkg/testutil"
    14  )
    15  
    16  var tests = []struct {
    17  	flags []string
    18  	out   string
    19  }{
    20  	{
    21  		flags: []string{},
    22  		out: `d1
    23  f1
    24  f2
    25  f3?line 2
    26  `,
    27  	}, {
    28  		flags: []string{"-Q"},
    29  		out: `"d1"
    30  "f1"
    31  "f2"
    32  "f3\nline 2"
    33  `,
    34  	}, {
    35  		flags: []string{"-aR"},
    36  		out: `.
    37  .f4
    38  d1
    39  d1/f4
    40  f1
    41  f2
    42  f3?line 2
    43  `,
    44  	}, {
    45  		flags: []string{"-R"},
    46  		out: `d1
    47  d1/f4
    48  f1
    49  f2
    50  f3?line 2
    51  `,
    52  	}, {
    53  		flags: []string{"-a"},
    54  		out: `.
    55  .f4
    56  d1
    57  f1
    58  f2
    59  f3?line 2
    60  `,
    61  	},
    62  }
    63  
    64  func TestLs(t *testing.T) {
    65  	tmpDir, err := ioutil.TempDir("", "ls")
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	defer os.RemoveAll(tmpDir)
    70  
    71  	// Create an empty directory.
    72  	testDir := filepath.Join(tmpDir, "testDir")
    73  	os.Mkdir(testDir, 0700)
    74  
    75  	// Create some files.
    76  	os.Create(filepath.Join(testDir, "f1"))
    77  	os.Create(filepath.Join(testDir, "f2"))
    78  	os.Create(filepath.Join(testDir, "f3\nline 2"))
    79  	os.Create(filepath.Join(testDir, ".f4"))
    80  	os.Mkdir(filepath.Join(testDir, "d1"), 0740)
    81  	os.Create(filepath.Join(testDir, "d1/f4"))
    82  
    83  	// Table-driven testing
    84  	for _, tt := range tests {
    85  		c := testutil.Command(t, tt.flags...)
    86  		c.Dir = testDir
    87  		out, err := c.Output()
    88  		if err != nil {
    89  			t.Error(err)
    90  		}
    91  		if string(out) != tt.out {
    92  			t.Errorf("got:\n%s\nwant:\n%s", string(out), tt.out)
    93  		}
    94  	}
    95  }
    96  
    97  func TestMain(m *testing.M) {
    98  	testutil.Run(m, main)
    99  }