github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/cmds/core/dirname/dirname_test.go (about)

     1  // Copyright 2016 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  	"testing"
    10  
    11  	"github.com/u-root/u-root/pkg/testutil"
    12  )
    13  
    14  type test struct {
    15  	args []string
    16  	out  string
    17  	err  string
    18  }
    19  
    20  var dirnameTests = []test{
    21  	// For no args it seems we have to print an error.
    22  	// It should be missing operand[s] but that's not the standard.
    23  	{args: []string{}, err: "dirname: missing operand\n"},
    24  	{args: []string{""}, out: ".\n"},
    25  	{args: []string{"/this/that"}, out: "/this\n"},
    26  	{args: []string{"/this/that", "/other"}, out: "/this\n/\n"},
    27  	{args: []string{"/this/that", "/other thing/space"}, out: "/this\n/other thing\n"},
    28  }
    29  
    30  func TestDirName(t *testing.T) {
    31  	// Table-driven testing
    32  	for _, tt := range dirnameTests {
    33  		c := testutil.Command(t, tt.args...)
    34  		stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
    35  		c.Stdout, c.Stderr = stdout, stderr
    36  		err := c.Run()
    37  		if err != nil && tt.err == "" {
    38  			t.Errorf("Test %v: got %q, want nil", tt.args, err)
    39  			continue
    40  		}
    41  
    42  		t.Logf("RUN: %v: got %q, %q", tt.args, stdout, stderr)
    43  		if stdout.String() != tt.out {
    44  			t.Errorf("%v: stdout got %q, wants %q", tt.args, stdout.String(), tt.out)
    45  		}
    46  		if err != nil && stderr.String() != "" && stderr.String()[len("yyyy/mm/dd hh:mm:ss "):] != tt.err {
    47  			t.Errorf("%v: stderr got %q, wants %q", tt.args, stderr.String(), tt.err)
    48  		}
    49  
    50  	}
    51  }
    52  
    53  func TestMain(m *testing.M) {
    54  	testutil.Run(m, main)
    55  }