github.com/rck/u-root@v0.0.0-20180106144920-7eb602e381bb/cmds/which/which_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  // created by Rafael Campos Nunes <rafaelnunes@engineer.com>
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"os"
    12  	"os/exec"
    13  	"reflect"
    14  	"testing"
    15  )
    16  
    17  type commands struct {
    18  	name      string
    19  	pathOnSys []byte
    20  }
    21  
    22  // in setup we fill the pathOnSys variables with their corresponding path on the system.
    23  var (
    24  	tests = []commands{
    25  		{
    26  			"cat",
    27  			[]byte{},
    28  		},
    29  		{
    30  			"which",
    31  			[]byte{},
    32  		},
    33  		{
    34  			"sed",
    35  			[]byte{},
    36  		},
    37  		{
    38  			"ldd",
    39  			[]byte{},
    40  		},
    41  	}
    42  
    43  	p = os.Getenv("PATH")
    44  )
    45  
    46  func setup() error {
    47  	var err error
    48  
    49  	for i, t := range tests {
    50  		tests[i].pathOnSys, err = exec.Command("which", t.name).Output()
    51  		if err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  // TestWhichUnique tests `which` command against one POSIX command that are included in Linux.
    60  // The output of which.go has to be exactly equal to the output of which itself.
    61  func TestWhichUnique(t *testing.T) {
    62  	err := setup()
    63  
    64  	if err != nil {
    65  		t.Fatalf("setup has failed, %v", err)
    66  	}
    67  
    68  	commands := []string{"cat"}
    69  	var b bytes.Buffer
    70  	which(p, &b, commands[:])
    71  
    72  	// Comparing against only the cat command.
    73  	if !reflect.DeepEqual(b.Bytes(), tests[0].pathOnSys) {
    74  		t.Fatalf("Locating commands has failed, wants: %v, got: %v", string(tests[0].pathOnSys), string(b.Bytes()))
    75  	}
    76  }
    77  
    78  // TestWhichMultiple tests `which` command against the three POSIX commands that are included in Linux.
    79  // The output of which.go has to be exactly equal to the output of which itself. If it works with
    80  // three, it should work with more commands as well.
    81  func TestWhichMultiple(t *testing.T) {
    82  	err := setup()
    83  
    84  	if err != nil {
    85  		t.Fatalf("setup has failed, %v", err)
    86  	}
    87  
    88  	pathsCombined := []byte{}
    89  	commands := []string{}
    90  	for _, t := range tests {
    91  		pathsCombined = append(pathsCombined, t.pathOnSys...)
    92  		commands = append(commands, t.name)
    93  	}
    94  
    95  	var b bytes.Buffer
    96  	which(p, &b, commands[:])
    97  
    98  	if !reflect.DeepEqual(b.Bytes(), pathsCombined) {
    99  		t.Fatalf("Locating commands has failed, wants: %v, got: %v", string(pathsCombined), string(b.Bytes()))
   100  	}
   101  }