gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/basename/basename_test.go (about)

     1  // Copyright 2018 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  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/u-root/u-root/pkg/testutil"
    13  )
    14  
    15  type test struct {
    16  	flags      []string
    17  	out        string
    18  	stdErr     string
    19  	exitStatus int
    20  }
    21  
    22  func TestBasename(t *testing.T) {
    23  
    24  	var tests = []test{
    25  		{
    26  			flags:      []string{"foo.h", ".h"},
    27  			out:        "foo\n",
    28  			stdErr:     "",
    29  			exitStatus: 0,
    30  		},
    31  		{
    32  			flags:      []string{"/bar/baz/biz/foo.h", "bar/baz/biz"},
    33  			out:        "foo.h\n",
    34  			stdErr:     "",
    35  			exitStatus: 0,
    36  		},
    37  		{
    38  			flags:      []string{".h", ".h"},
    39  			out:        ".h\n",
    40  			stdErr:     "",
    41  			exitStatus: 0,
    42  		},
    43  		{
    44  			flags:      []string{"/some/path/foo"},
    45  			out:        "foo\n",
    46  			stdErr:     "",
    47  			exitStatus: 0,
    48  		},
    49  	}
    50  
    51  	// Table-driven testing
    52  	for _, tt := range tests {
    53  		t.Run(fmt.Sprintf("Using flags %s", tt.flags), func(t *testing.T) {
    54  			var out, stdErr bytes.Buffer
    55  			cmd := testutil.Command(t, tt.flags...)
    56  			cmd.Stdout = &out
    57  			cmd.Stderr = &stdErr
    58  			err := cmd.Run()
    59  
    60  			if out.String() != tt.out {
    61  				t.Errorf("stdout got:\n%s\nwant:\n%s", out.String(), tt.out)
    62  			}
    63  
    64  			if stdErr.String() != tt.stdErr {
    65  				t.Errorf("stderr got:\n%s\nwant:\n%s", stdErr.String(), tt.stdErr)
    66  			}
    67  
    68  			if tt.exitStatus == 0 && err != nil {
    69  				t.Errorf("expected to exit with %d, but exited with err %s", tt.exitStatus, err)
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func TestMain(m *testing.M) {
    76  	testutil.Run(m, main)
    77  }