github.com/derekparker/go-pear@v1.3.2/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/libgit2/git2go"
     9  )
    10  
    11  func TestPear(t *testing.T) {
    12  	mockHomeEnv("fixtures/integration")
    13  	tmpstdin := mockStdin(t, "Person B")
    14  	tmp, oldstdout := mockStdout(t)
    15  	pearrc := createPearrc(t, []byte("email: foo@example.com\ndevs:\n  deva: Full Name A"))
    16  	defer func() {
    17  		cleanupStdout(t, tmp, oldstdout)
    18  		closeFile(tmpstdin)
    19  		closeFile(pearrc)
    20  	}()
    21  
    22  	os.Args = []string{"pear", "DevB", "DevA", "--file", "fixtures/test.config"}
    23  
    24  	main()
    25  
    26  	conf, err := readPearrc("fixtures/integration/.pearrc")
    27  	if err != nil {
    28  		t.Error(err)
    29  	}
    30  
    31  	if len(conf.Devs) != 2 {
    32  		t.Error("Devs were not recorded")
    33  	}
    34  
    35  	expectedUser := "Full Name A and Person B"
    36  	gitconfig := initTestGitConfig("fixtures/test.config", t)
    37  	actualUser := username(gitconfig)
    38  	if actualUser != expectedUser {
    39  		t.Errorf("Expected %s got %s", expectedUser, actualUser)
    40  	}
    41  
    42  	expectedEmail := "foo+deva+devb@example.com"
    43  	actualEmail := email(gitconfig)
    44  	if actualEmail != expectedEmail {
    45  		t.Errorf("Expected %s got %s", expectedEmail, actualEmail)
    46  	}
    47  }
    48  
    49  func TestPearOneDevNoSavedEmail(t *testing.T) {
    50  	mockHomeEnv("fixtures/integration")
    51  	tmpstdin := mockStdin(t, "dev@pear.biz")
    52  	tmp, oldstdout := mockStdout(t)
    53  
    54  	pearrc := createPearrc(t, []byte("devs:\n  dev1: Full Name A"))
    55  	defer func() {
    56  		cleanupStdout(t, tmp, oldstdout)
    57  		closeFile(tmpstdin)
    58  		closeFile(pearrc)
    59  	}()
    60  
    61  	os.Args = []string{"pear", "dev1", "--email", "foo@biz.net", "--file", "fixtures/test.config"}
    62  
    63  	main()
    64  
    65  	readConf, err := readPearrc("fixtures/integration/.pearrc")
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	if readConf.Email != "dev@pear.biz" {
    71  		t.Error("Email was not saved.")
    72  	}
    73  }
    74  
    75  func TestPearWithinSubdirectory(t *testing.T) {
    76  	mockHomeEnv("fixtures/integration")
    77  	pearrc := createPearrc(t, []byte("email: foo@example.com\ndevs:\n  deva: Full Name A\n  devb: Full Name B"))
    78  	defer closeFile(pearrc)
    79  
    80  	withinStubRepo(t, "foo", func(conf *git.Config) {
    81  		err := os.MkdirAll("bar", os.ModePerm|os.ModeExclusive|os.ModeDir)
    82  		if err != nil {
    83  			t.Fatal(err)
    84  		}
    85  
    86  		err = os.Chdir("bar")
    87  		if err != nil {
    88  			t.Fatal(err)
    89  		}
    90  
    91  		os.Args = []string{"pear", "DevB", "DevA"}
    92  		main()
    93  
    94  		conf.Refresh()
    95  
    96  		expected := "Full Name A and Full Name B"
    97  		if usr := username(conf); usr != expected {
    98  			t.Errorf("Expected %s, got %s", expected, usr)
    99  		}
   100  	})
   101  }
   102  
   103  func TestCheckEmail(t *testing.T) {
   104  	conf := Config{}
   105  
   106  	mockHomeEnv("fixtures/integration")
   107  	tempstdin := mockStdin(t, "dev@pear.biz")
   108  	tmp, oldstdout := mockStdout(t)
   109  	pearrc := createPearrc(t, []byte("devs:\n  dev1: Full Name A"))
   110  
   111  	defer func() {
   112  		cleanupStdout(t, tmp, oldstdout)
   113  		closeFile(tempstdin)
   114  		closeFile(pearrc)
   115  	}()
   116  
   117  	checkEmail(&conf)
   118  
   119  	_, err := tmp.Seek(0, os.SEEK_SET)
   120  	if err != nil {
   121  		t.Error(err)
   122  	}
   123  
   124  	output, err := ioutil.ReadAll(tmp)
   125  	if err != nil {
   126  		t.Error("Could not read from temp file")
   127  	}
   128  
   129  	if string(output) != "Please provide base author email:\n" {
   130  		t.Errorf("Prompt was incorrect, got: %#v", string(output))
   131  	}
   132  
   133  	expected := "dev@pear.biz"
   134  	if conf.Email != expected {
   135  		t.Errorf("Expected %s, got %s", expected, conf.Email)
   136  	}
   137  }
   138  
   139  func TestSetPairWithOneDev(t *testing.T) {
   140  	gitconfig := initTestGitConfig("fixtures/test.config", t)
   141  
   142  	setPair("foo@example.com", []string{"user1"}, gitconfig)
   143  	expected := "user1"
   144  	actual := username(gitconfig)
   145  
   146  	if actual != expected {
   147  		t.Errorf("Expected %s got %s", expected, actual)
   148  	}
   149  }
   150  
   151  func TestSetPairWithTwoDevs(t *testing.T) {
   152  	pair := []string{"user1", "user2"}
   153  	formattedEmail := formatEmail("dev@example.com", pair)
   154  	gitconfig := initTestGitConfig("fixtures/test.config", t)
   155  
   156  	setPair(formattedEmail, pair, gitconfig)
   157  	expectedUser := "user1 and user2"
   158  	actualUser := username(gitconfig)
   159  	expectedEmail := "dev+user1+user2@example.com"
   160  	actualEmail := email(gitconfig)
   161  
   162  	if actualUser != expectedUser {
   163  		t.Errorf("Expected %s got %s", expectedUser, actualUser)
   164  	}
   165  
   166  	if actualEmail != expectedEmail {
   167  		t.Errorf("Expected %s got %s", expectedEmail, actualEmail)
   168  	}
   169  }
   170  
   171  func TestReadPearrc(t *testing.T) {
   172  	nonExistantPath := "fixtures/.fakepearrc"
   173  
   174  	readPearrc(nonExistantPath)
   175  
   176  	f, err := os.Open(nonExistantPath)
   177  	if err != nil {
   178  		t.Error(err)
   179  	}
   180  
   181  	defer func() {
   182  		f.Close()
   183  		os.Remove(nonExistantPath)
   184  	}()
   185  }
   186  
   187  func TestSavePearrc(t *testing.T) {
   188  	expected := map[string]string{
   189  		"dparker":   "Derek Parker",
   190  		"chriserin": "Chris Erin",
   191  	}
   192  
   193  	conf := Config{
   194  		Devs: expected,
   195  	}
   196  
   197  	err := savePearrc(&conf, "fixtures/.pearrc")
   198  	if err != nil {
   199  		t.Fatal(err)
   200  	}
   201  
   202  	readConf, err := readPearrc("fixtures/.pearrc")
   203  	if err != nil {
   204  		t.Fatal(err)
   205  	}
   206  
   207  	actual := readConf.Devs
   208  	if len(actual) != len(expected) {
   209  		t.Error("Did not read devs")
   210  	}
   211  
   212  	for username, dev := range expected {
   213  		if actual[username] != dev {
   214  			t.Errorf("Expected %s got %s", dev, actual[username])
   215  		}
   216  	}
   217  }
   218  
   219  func TestCheckPairWithUnknownDev(t *testing.T) {
   220  	expectedFullName := "Person B"
   221  	pair := []string{"knowndev", "newdev"}
   222  	conf := &Config{
   223  		Devs: map[string]string{
   224  			"knowndev": "Known Dev",
   225  		},
   226  	}
   227  
   228  	tmpstdin := mockStdin(t, "Person B")
   229  	tmp, oldstdout := mockStdout(t)
   230  	defer func() {
   231  		cleanupStdout(t, tmp, oldstdout)
   232  		closeFile(tmpstdin)
   233  	}()
   234  	checkPair(pair, conf)
   235  
   236  	_, err := tmp.Seek(0, os.SEEK_SET)
   237  	if err != nil {
   238  		t.Error(err)
   239  	}
   240  
   241  	output, err := ioutil.ReadAll(tmp)
   242  	if err != nil {
   243  		t.Error("Could not read from temp file")
   244  	}
   245  
   246  	if string(output) != "Please enter a full name for newdev:\n" {
   247  		t.Errorf("Question output was incorrect, got: %v", string(output))
   248  	}
   249  
   250  	fullName, ok := conf.Devs["newdev"]
   251  	if !ok {
   252  		t.Error("Dev was not found in conf")
   253  	}
   254  
   255  	if fullName != expectedFullName {
   256  		t.Errorf("Expected %s got %s", expectedFullName, fullName)
   257  	}
   258  }
   259  
   260  func TestEmailFormat(t *testing.T) {
   261  	tests := []struct {
   262  		email    string
   263  		devs     []string
   264  		expected string
   265  	}{
   266  		{"dev@example.com", []string{"dev1"}, "dev+dev1@example.com"},
   267  		{"dev@example.com", []string{"dev1", "dev2"}, "dev+dev1+dev2@example.com"},
   268  		{"dev@example.com", []string{"dev1", "dev2", "dev3"}, "dev+dev1+dev2+dev3@example.com"},
   269  	}
   270  
   271  	for _, test := range tests {
   272  		actual := formatEmail(test.email, test.devs)
   273  
   274  		if actual != test.expected {
   275  			t.Errorf("Expected %s, got %s", test.expected, actual)
   276  		}
   277  	}
   278  }