github.com/kamontat/dot-github@v1.2.0/git_test.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/mitchellh/go-homedir"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestGitHubRemoteURL(t *testing.T) {
    12  	o := GitHubRemoteURL("origin")
    13  	if !strings.Contains(o, "dot-github") {
    14  		t.Fatalf("'origin' remote url is invalid: %v", o)
    15  	}
    16  }
    17  
    18  func TestGitHubRemoteURLWithEnvVar(t *testing.T) {
    19  	os.Setenv("DOT_GITHUB_GIT_CMD", "git")
    20  	o := GitHubRemoteURL("origin")
    21  	if !strings.Contains(o, "dot-github") {
    22  		t.Fatalf("'origin' remote url is invalid: %v", o)
    23  	}
    24  	os.Setenv("DOT_GITHUB_GIT_CMD", "")
    25  }
    26  
    27  func TestGitHubRemoteURLWithInvalidEnvVar(t *testing.T) {
    28  	os.Setenv("DOT_GITHUB_GIT_CMD", "unknown-command")
    29  	defer func() {
    30  		if r := recover(); r == nil {
    31  			t.Errorf("Uknown command for Git must cause panic")
    32  		}
    33  		os.Setenv("DOT_GITHUB_GIT_CMD", "")
    34  	}()
    35  	GitHubRemoteURL("origin")
    36  }
    37  
    38  func TestInvalidRemote(t *testing.T) {
    39  	defer func() {
    40  		if r := recover(); r == nil {
    41  			t.Errorf("Uknown remote must cause panic")
    42  		}
    43  	}()
    44  	GitHubRemoteURL("uknown-remote-name")
    45  }
    46  
    47  func TestGitRoot(t *testing.T) {
    48  	r := GitRoot()
    49  	if len(r) == 0 {
    50  		t.Fatalf("GitRoot() must return non-empty string")
    51  	}
    52  	if !filepath.IsAbs(r) {
    53  		t.Fatalf("GitRoot() must return absolute path but actually: %v", r)
    54  	}
    55  	if !strings.Contains(r, "dot-github") {
    56  		t.Fatalf("GitRoot() must return path to its repository but actually: %v", r)
    57  	}
    58  }
    59  
    60  func TestNonGitRepo(t *testing.T) {
    61  	home, err := homedir.Dir()
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	wd, _ := os.Getwd()
    66  	os.Chdir(home)
    67  	defer func() {
    68  		if err := recover(); err == nil {
    69  			t.Errorf("panic must occur when current dir is not Git repo")
    70  		}
    71  		os.Chdir(wd)
    72  	}()
    73  	GitRoot()
    74  }
    75  
    76  func TestValidateURLs(t *testing.T) {
    77  	var b bool
    78  	b = ValidateGitHubURL("http://github.com/rhysd/dot-github.git")
    79  	if !b {
    80  		t.Errorf("Correct HTTP GitHub URL was detected as invalid URL")
    81  	}
    82  	b = ValidateGitHubURL("https://github.com/rhysd/dot-github.git")
    83  	if !b {
    84  		t.Errorf("Correct HTTPS GitHub URL was detected as invalid URL")
    85  	}
    86  	b = ValidateGitHubURL("git://github.com/rhysd/dot-github.git")
    87  	if !b {
    88  		t.Errorf("Correct GIT GitHub URL was detected as invalid URL")
    89  	}
    90  	b = ValidateGitHubURL("git@github.com:rhysd/dot-github.git")
    91  	if !b {
    92  		t.Errorf("Correct SSH GitHub URL was detected as invalid URL")
    93  	}
    94  	b = ValidateGitHubURL("http://github.company.com/rhysd/dot-github.git")
    95  	if !b {
    96  		t.Errorf("Correct HTTP GHE URL was detected as invalid URL")
    97  	}
    98  	b = ValidateGitHubURL("https://github.company.com/rhysd/dot-github.git")
    99  	if !b {
   100  		t.Errorf("Correct HTTPS GHE URL was detected as invalid URL")
   101  	}
   102  	b = ValidateGitHubURL("git://github.company.com/rhysd/dot-github.git")
   103  	if !b {
   104  		t.Errorf("Correct GIT GHE URL was detected as invalid URL")
   105  	}
   106  	b = ValidateGitHubURL("git@github.company.com:rhysd/dot-github.git")
   107  	if !b {
   108  		t.Errorf("Correct SSH GHE URL was detected as invalid URL")
   109  	}
   110  }
   111  
   112  func TestInvalidateURLs(t *testing.T) {
   113  	var b bool
   114  	b = ValidateGitHubURL("http://example.com/rhysd/dot-example.git")
   115  	if b {
   116  		t.Errorf("Correct HTTP GitHub URL was detected as invalid URL")
   117  	}
   118  	b = ValidateGitHubURL("https://example.com/rhysd/dot-example.git")
   119  	if b {
   120  		t.Errorf("Correct HTTPS GitHub URL was detected as invalid URL")
   121  	}
   122  	b = ValidateGitHubURL("git://example.com/rhysd/dot-example.git")
   123  	if b {
   124  		t.Errorf("Correct GIT GitHub URL was detected as invalid URL")
   125  	}
   126  	b = ValidateGitHubURL("git@example.com:rhysd/dot-example.git")
   127  	if b {
   128  		t.Errorf("Correct SSH GitHub URL was detected as invalid URL")
   129  	}
   130  	b = ValidateGitHubURL("http://example.company.com/rhysd/dot-example.git")
   131  	if b {
   132  		t.Errorf("Correct HTTP GHE URL was detected as invalid URL")
   133  	}
   134  	b = ValidateGitHubURL("https://example.company.com/rhysd/dot-example.git")
   135  	if b {
   136  		t.Errorf("Correct HTTPS GHE URL was detected as invalid URL")
   137  	}
   138  	b = ValidateGitHubURL("git://example.company.com/rhysd/dot-example.git")
   139  	if b {
   140  		t.Errorf("Correct GIT GHE URL was detected as invalid URL")
   141  	}
   142  	b = ValidateGitHubURL("git@example.company.com:rhysd/dot-example.git")
   143  	if b {
   144  		t.Errorf("Correct SSH GHE URL was detected as invalid URL")
   145  	}
   146  }