github.com/emmahsax/go-git-helper@v0.0.8-0.20240519163017-907b9de0fa52/internal/githubPullRequest/githubPullRequest_test.go (about)

     1  package githubPullRequest
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/emmahsax/go-git-helper/internal/commandline"
    10  )
    11  
    12  func Test_newPrBody(t *testing.T) {
    13  	tempDir, err := os.MkdirTemp("", "github")
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  	defer os.RemoveAll(tempDir)
    18  
    19  	err = os.MkdirAll(filepath.Join(tempDir, ".github", "PULL_REQUEST_TEMPLATE"), 0755)
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  
    24  	files := []string{
    25  		".github/pull_request_template.md",
    26  		".github/PULL_REQUEST_TEMPLATE/template1.md",
    27  		".github/PULL_REQUEST_TEMPLATE/template2.md",
    28  		"pull_request_template.md",
    29  	}
    30  	for _, file := range files {
    31  		err = os.WriteFile(filepath.Join(tempDir, file), []byte("content"), 0644)
    32  		if err != nil {
    33  			t.Fatal(err)
    34  		}
    35  	}
    36  
    37  	mr := NewGitHubPullRequest(
    38  		map[string]string{
    39  			"baseBranch":  "master",
    40  			"localRepo":   "repo",
    41  			"localBranch": "feature",
    42  			"gitRootDir":  tempDir,
    43  			"newPrTitle":  "hello world",
    44  			"draft":       "false",
    45  		},
    46  		false,
    47  	)
    48  
    49  	originalAskYesNoQuestion := commandline.AskYesNoQuestion
    50  	t.Cleanup(func() {
    51  		commandline.AskYesNoQuestion = originalAskYesNoQuestion
    52  	})
    53  	commandline.AskYesNoQuestion = func(question string) bool {
    54  		return true
    55  	}
    56  
    57  	originalAskMultipleChoice := commandline.AskMultipleChoice
    58  	t.Cleanup(func() {
    59  		commandline.AskMultipleChoice = originalAskMultipleChoice
    60  	})
    61  	commandline.AskMultipleChoice = func(question string, choices []string) string {
    62  		return ""
    63  	}
    64  
    65  	expected := ""
    66  	actual := mr.newPrBody()
    67  
    68  	if expected != actual {
    69  		t.Errorf("expected '%s', got '%s'", expected, actual)
    70  	}
    71  }
    72  
    73  func Test_templateNameToApply(t *testing.T) {
    74  	tempDir, err := os.MkdirTemp("", "github")
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	defer os.RemoveAll(tempDir)
    79  
    80  	err = os.MkdirAll(filepath.Join(tempDir, ".github", "PULL_REQUEST_TEMPLATE"), 0755)
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	files := []string{
    86  		".github/pull_request_template.md",
    87  		".github/PULL_REQUEST_TEMPLATE/template1.md",
    88  		".github/PULL_REQUEST_TEMPLATE/template2.md",
    89  		"pull_request_template.md",
    90  	}
    91  	for _, file := range files {
    92  		err = os.WriteFile(filepath.Join(tempDir, file), []byte("content"), 0644)
    93  		if err != nil {
    94  			t.Fatal(err)
    95  		}
    96  	}
    97  
    98  	mr := &GitHubPullRequest{
    99  		GitRootDir: tempDir,
   100  	}
   101  
   102  	originalAskYesNoQuestion := commandline.AskYesNoQuestion
   103  	t.Cleanup(func() {
   104  		commandline.AskYesNoQuestion = originalAskYesNoQuestion
   105  	})
   106  	commandline.AskYesNoQuestion = func(question string) bool {
   107  		return true
   108  	}
   109  
   110  	originalAskMultipleChoice := commandline.AskMultipleChoice
   111  	t.Cleanup(func() {
   112  		commandline.AskMultipleChoice = originalAskMultipleChoice
   113  	})
   114  	commandline.AskMultipleChoice = func(question string, choices []string) string {
   115  		return "/path/to/repo/.github/pull_request_template.md"
   116  	}
   117  
   118  	expected := "/path/to/repo/.github/pull_request_template.md"
   119  	actual := mr.templateNameToApply()
   120  
   121  	if expected != actual {
   122  		t.Errorf("expected '%s', got '%s'", expected, actual)
   123  	}
   124  }
   125  
   126  func Test_determineTemplate(t *testing.T) {
   127  	mr := &GitHubPullRequest{
   128  		GitRootDir: "/path/to/repo",
   129  	}
   130  
   131  	originalAskYesNoQuestion := commandline.AskYesNoQuestion
   132  	t.Cleanup(func() {
   133  		commandline.AskYesNoQuestion = originalAskYesNoQuestion
   134  	})
   135  	commandline.AskYesNoQuestion = func(question string) bool {
   136  		return true
   137  	}
   138  
   139  	originalAskMultipleChoice := commandline.AskMultipleChoice
   140  	t.Cleanup(func() {
   141  		commandline.AskMultipleChoice = originalAskMultipleChoice
   142  	})
   143  	commandline.AskMultipleChoice = func(question string, choices []string) string {
   144  		return "/path/to/repo/.github/pull_request_template.md"
   145  	}
   146  
   147  	expected := "/path/to/repo/.github/pull_request_template.md"
   148  	actual := mr.determineTemplate()
   149  
   150  	if expected != actual {
   151  		t.Errorf("expected '%s', got '%s'", expected, actual)
   152  	}
   153  }
   154  
   155  func Test_prTemplateOptions(t *testing.T) {
   156  	tempDir, err := os.MkdirTemp("", "github")
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	defer os.RemoveAll(tempDir)
   161  
   162  	err = os.MkdirAll(filepath.Join(tempDir, ".github", "PULL_REQUEST_TEMPLATE"), 0755)
   163  	if err != nil {
   164  		t.Fatal(err)
   165  	}
   166  
   167  	files := []string{
   168  		".github/pull_request_template.md",
   169  		".github/PULL_REQUEST_TEMPLATE/template1.md",
   170  		".github/PULL_REQUEST_TEMPLATE/template2.md",
   171  		"pull_request_template.md",
   172  	}
   173  	for _, file := range files {
   174  		err = os.WriteFile(filepath.Join(tempDir, file), []byte("content"), 0644)
   175  		if err != nil {
   176  			t.Fatal(err)
   177  		}
   178  	}
   179  
   180  	mr := &GitHubPullRequest{
   181  		GitRootDir: tempDir,
   182  	}
   183  
   184  	expected := []string{
   185  		filepath.Join(tempDir, ".github/pull_request_template.md"),
   186  		filepath.Join(tempDir, ".github/PULL_REQUEST_TEMPLATE/template1.md"),
   187  		filepath.Join(tempDir, ".github/PULL_REQUEST_TEMPLATE/template2.md"),
   188  		filepath.Join(tempDir, "pull_request_template.md"),
   189  	}
   190  	actual := mr.prTemplateOptions()
   191  
   192  	if len(expected) != len(actual) {
   193  		t.Fatalf("Expected and actual slices have different lengths: expected %v, got %v", len(expected), len(actual))
   194  	}
   195  
   196  	sort.Strings(expected)
   197  	sort.Strings(actual)
   198  
   199  	for i := range expected {
   200  		if expected[i] != actual[i] {
   201  			t.Fatalf("Expected and actual slices do not match: expected %v, got %v", expected, actual)
   202  		}
   203  	}
   204  }