github.com/nxadm/ctwrapper@v0.5.3-0.20200107113753-fb73fb7c1f50/fs_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  func TestFindConsulTemplate(t *testing.T) {
    10  	// Manipulate the PATH
    11  	osPath := os.Getenv("PATH")
    12  	cwd, _ := os.Getwd()
    13  	defer os.Setenv("PATH", osPath)
    14  	os.Setenv("PATH", "")
    15  
    16  	// Test exec in cwd
    17  	os.Chdir("t")
    18  	ct, err := findConsulTemplate()
    19  	if err != nil {
    20  		t.Error("Can't find executable in the working directory")
    21  	} else if filepath.Base(ct) != "consul-template" {
    22  		t.Errorf("Wrong executable found in the working directory; %s", ct)
    23  	}
    24  
    25  	// Test exec in PATH
    26  	os.Chdir(cwd)
    27  	os.Setenv("PATH", "t")
    28  	if err != nil {
    29  		t.Error("Can't find executable in the PATH")
    30  	} else if filepath.Base(ct) != "consul-template" {
    31  		t.Errorf("Wrong executable found in the working directory; %s", ct)
    32  	}
    33  
    34  }
    35  
    36  func TestFindTemplates(t *testing.T) {
    37  	templates, err := findTemplates("t", defaultExt)
    38  	if err != nil {
    39  		t.Errorf("Unexpected error found: %s", err)
    40  	}
    41  
    42  	if len(templates) != len(templatesTest) {
    43  		t.Errorf("Incorrect number of templates, got: %d, want: %d.", len(templates), len(templatesTest))
    44  	}
    45  	for _, elem := range templatesTest {
    46  		if !stringInSlice(elem, templates) {
    47  			t.Errorf("\"%s\" not found in the predefined data (found files: %+v)", elem, templates)
    48  		}
    49  	}
    50  }