github.com/grantbow/fit@v0.7.1-0.20220916164603-1f7c88ac81e6/scm/Detect_test.go (about)

     1  package scm
     2  
     3  import (
     4  	bugs "github.com/grantbow/fit/issues"
     5  	"io/ioutil"
     6  	"os"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestDetectGit(t *testing.T) {
    12  	var config bugs.Config
    13  	config.DescriptionFileName = "Description"
    14  	var gdir string
    15  	gdir, err := ioutil.TempDir("", "gitdetect")
    16  	pwd, _ := os.Getwd()
    17  	if err == nil {
    18  		os.Chdir(gdir)
    19  		// Hack to get around the fact that /tmp is a symlink on
    20  		// OS X, and it causes the directory checks to fail..
    21  		gdir, _ = os.Getwd()
    22  		defer os.RemoveAll(gdir)
    23  	} else {
    24  		t.Error("Failed creating temporary directory for detect")
    25  		return
    26  	}
    27  	// Fake a git repo
    28  	os.Mkdir(".git", 0755)
    29  
    30  	options := make(map[string]bool)
    31  	handler, dir, err := DetectSCM(options, config)
    32  	if err != nil {
    33  		t.Error("Unexpected while detecting repo type: " + err.Error())
    34  	}
    35  	if dir != bugs.Directory(gdir+sops+".git") {
    36  		t.Error("Unexpected directory found when trying to detect git repo" + dir)
    37  	}
    38  	switch handler.(type) {
    39  	case GitManager:
    40  		// GitManager is what we expect, don't fall through
    41  		// to the error
    42  	default:
    43  		t.Error("Unexpected SCMHandler found for Git")
    44  	}
    45  
    46  	// Go somewhere higher in the tree and do it again
    47  	os.MkdirAll("tmp"+sops+"abc"+sops+"hello", 0755)
    48  	os.Chdir("tmp" + sops + "abc" + sops + "hello")
    49  	handler, dir, err = DetectSCM(options, config)
    50  	if err != nil {
    51  		t.Error("Unexpected while detecting repo type: " + err.Error())
    52  	}
    53  	if dir != bugs.Directory(gdir+sops+".git") {
    54  		t.Error("Unexpected directory found when trying to detect git repo" + dir)
    55  	}
    56  	switch handler.(type) {
    57  	case GitManager:
    58  		// GitManager is what we expect, don't fall through
    59  		// to the error
    60  	default:
    61  		t.Error("Unexpected SCMHandler found for Git")
    62  	}
    63  	os.Chdir(pwd)
    64  }
    65  
    66  func TestDetectHg(t *testing.T) {
    67  	var config bugs.Config
    68  	config.DescriptionFileName = "Description"
    69  	var gdir string
    70  	gdir, err := ioutil.TempDir("", "hgdetect")
    71  	pwd, _ := os.Getwd()
    72  	if err == nil {
    73  		os.Chdir(gdir)
    74  		// Hack to get around the fact that /tmp is a symlink on
    75  		// OS X, and it causes the directory checks to fail..
    76  		gdir, _ = os.Getwd()
    77  		defer os.RemoveAll(gdir)
    78  	} else {
    79  		t.Error("Failed creating temporary directory for detect")
    80  		return
    81  	}
    82  	// Fake an hg repo
    83  	os.Mkdir(".hg", 0755)
    84  
    85  	options := make(map[string]bool)
    86  	handler, dir, err := DetectSCM(options, config)
    87  	if err != nil {
    88  		t.Error("Unexpected while detecting repo type: " + err.Error())
    89  	}
    90  	if dir != bugs.Directory(gdir+sops+".hg") {
    91  		t.Error("Unexpected directory found when trying to detect hg repo" + dir)
    92  	}
    93  	switch handler.(type) {
    94  	case HgManager:
    95  		// HgManager is what we expect, don't fall through
    96  		// to the error
    97  	default:
    98  		t.Error("Unexpected SCMHandler found for Mercurial")
    99  	}
   100  
   101  	// Go somewhere higher in the tree and do it again
   102  	os.MkdirAll("tmp"+sops+"abc"+sops+"hello", 0755)
   103  	os.Chdir("tmp" + sops + "abc" + sops + "hello")
   104  	handler, dir, err = DetectSCM(options, config)
   105  	if err != nil {
   106  		t.Error("Unexpected while detecting repo type: " + err.Error())
   107  	}
   108  	if dir != bugs.Directory(gdir+sops+".hg") {
   109  		t.Error("Unexpected directory found when trying to detect hg repo" + dir)
   110  	}
   111  	switch handler.(type) {
   112  	case HgManager:
   113  		// HgManager is what we expect, don't fall through
   114  		// to the error
   115  	default:
   116  		t.Error("Unexpected SCMHandler found for Mercurial")
   117  	}
   118  	os.Chdir(pwd)
   119  }
   120  
   121  func TestDetectNone(t *testing.T) {
   122  	t.Skip("windows failure - see scm/Detect_test.go+121")
   123  	// TODO: finish making tests on Windows pass then redo this test
   124  	// seems to be run below a directory with a .git
   125  
   126  	var config bugs.Config
   127  	config.DescriptionFileName = "Description"
   128  	var gdir string
   129  	gdir, err := ioutil.TempDir("", "nonedetect") // almost same
   130  	pwd, _ := os.Getwd()
   131  	if err == nil {
   132  		os.Chdir(gdir)
   133  		// Hack to get around the fact that /tmp is a symlink on
   134  		// OS X, and it causes the directory checks to fail..
   135  		gdir, _ = os.Getwd()
   136  		defer os.RemoveAll(gdir)
   137  	} else {
   138  		t.Error("Failed creating temporary directory for detect")
   139  		return
   140  	}
   141  	// Fake an hg repo
   142  	//os.Mkdir(".hg", 0755)
   143  
   144  	options := make(map[string]bool)
   145  	handler, _, err := DetectSCM(options, config)
   146  	if err == nil {
   147  		t.Error("Unexpected success detecting repo type, no error.")
   148  	}
   149  	switch handler.(type) {
   150  	case nil:
   151  		// nil is what we expect, don't fall through
   152  		// to the error
   153  	default:
   154  		t.Error("Unexpected SCMHandler found, type : " + reflect.TypeOf(handler).String())
   155  	}
   156  	os.Chdir(pwd)
   157  }