github.com/grantbow/bug@v0.3.1/bugapp/Close_test.go (about)

     1  package bugapp
     2  
     3  import (
     4  	"fmt"
     5  	//	"io"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  // Captures stdout and stderr to ensure that
    12  // a usage line gets printed to Stderr when
    13  // no parameters are specified
    14  func TestCloseHelpOutput(t *testing.T) {
    15  
    16  	stdout, stderr := captureOutput(func() {
    17  		Close(ArgumentList{})
    18  	}, t)
    19  
    20  	if stdout != "" {
    21  		t.Error("Unexpected output on stdout.")
    22  	}
    23  	if stderr[:7] != "Usage: " {
    24  		t.Error("Expected usage information with no arguments")
    25  	}
    26  
    27  }
    28  
    29  // Test closing a bug given it's directory index
    30  func TestCloseByIndex(t *testing.T) {
    31  	dir, err := ioutil.TempDir("", "closetest")
    32  	defer os.RemoveAll(dir)
    33  	if err != nil {
    34  		t.Error("Could not create temporary dir for test")
    35  		return
    36  	}
    37  	os.Chdir(dir)
    38  	os.MkdirAll("issues/Test", 0700)
    39  
    40  	// On MacOS, /tmp is a symlink, which causes GetDirectory() to return
    41  	// a different path than expected in these tests, so make the issues
    42  	// directory explicit with an environment variable
    43  	err = os.Setenv("PMIT", dir)
    44  	if err != nil {
    45  		t.Error("Could not set environment variable: " + err.Error())
    46  		return
    47  	}
    48  
    49  	ioutil.WriteFile(dir+"/issues/Test/Identifier", []byte("TestBug\n"), 0600)
    50  
    51  	issuesDir, err := ioutil.ReadDir(fmt.Sprintf("%s/issues/", dir))
    52  	// Assert that there's 1 bug to start, otherwise what are we closing?
    53  	if err != nil || len(issuesDir) != 1 {
    54  		t.Error("Could not read issues directory")
    55  		return
    56  	}
    57  	stdout, stderr := captureOutput(func() {
    58  		Close(ArgumentList{"TestBug"})
    59  	}, t)
    60  	if stderr != "" {
    61  		t.Error("Unexpected output on STDERR for Test-bug")
    62  	}
    63  	if stdout != fmt.Sprintf("Removing %s/issues/Test\n", dir) {
    64  		t.Error("Unexpected output on STDOUT for Test-bug")
    65  		fmt.Printf("Got %s\nExpected%s", stdout, fmt.Sprintf("Removing %s/issues/Test\n", dir))
    66  	}
    67  	issuesDir, err = ioutil.ReadDir(fmt.Sprintf("%s/issues/", dir))
    68  	if err != nil {
    69  		t.Error("Could not read issues directory")
    70  		return
    71  	}
    72  	// After closing, there should be 0 bugs.
    73  	if len(issuesDir) != 0 {
    74  		t.Error("Unexpected number of issues in issues dir\n")
    75  	}
    76  }
    77  
    78  func TestCloseBugByIdentifier(t *testing.T) {
    79  	dir, err := ioutil.TempDir("", "close")
    80  	if err != nil {
    81  		t.Error("Could not create temporary dir for test")
    82  		return
    83  	}
    84  	os.Chdir(dir)
    85  	os.MkdirAll("issues/Test", 0700)
    86  	defer os.RemoveAll(dir)
    87  
    88  	// On MacOS, /tmp is a symlink, which causes GetDirectory() to return
    89  	// a different path than expected in these tests, so make the issues
    90  	// directory explicit with an environment variable
    91  	err = os.Setenv("PMIT", dir)
    92  	if err != nil {
    93  		t.Error("Could not set environment variable: " + err.Error())
    94  		return
    95  	}
    96  
    97  	issuesDir, err := ioutil.ReadDir(fmt.Sprintf("%s/issues/", dir))
    98  	// Assert that there's 1 bug to start, otherwise what are we closing?
    99  	if err != nil || len(issuesDir) != 1 {
   100  		t.Error("Could not read issues directory")
   101  		return
   102  	}
   103  	stdout, stderr := captureOutput(func() {
   104  		Close(ArgumentList{"1"})
   105  	}, t)
   106  	if stderr != "" {
   107  		t.Error("Unexpected output on STDERR for Test-bug")
   108  	}
   109  	if stdout != fmt.Sprintf("Removing %s/issues/Test\n", dir) {
   110  		t.Error("Unexpected output on STDOUT for Test-bug")
   111  		fmt.Printf("Got %s\nExpected: %s\n", stdout, dir)
   112  	}
   113  	issuesDir, err = ioutil.ReadDir(fmt.Sprintf("%s/issues/", dir))
   114  	if err != nil {
   115  		t.Error("Could not read issues directory")
   116  		return
   117  	}
   118  	// After closing, there should be 0 bugs.
   119  	if len(issuesDir) != 0 {
   120  		t.Error("Unexpected number of issues in issues dir\n")
   121  	}
   122  }