github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/githubtemplate/github_template_test.go (about)

     1  package githubtemplate
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestFindNonLegacy(t *testing.T) {
    11  	tmpdir, err := os.MkdirTemp("", "gh-cli")
    12  	if err != nil {
    13  		t.Fatal(err)
    14  	}
    15  
    16  	type args struct {
    17  		rootDir string
    18  		name    string
    19  	}
    20  	tests := []struct {
    21  		name    string
    22  		prepare []string
    23  		args    args
    24  		want    []string
    25  	}{
    26  		{
    27  			name: "Legacy templates ignored",
    28  			prepare: []string{
    29  				"README.md",
    30  				"ISSUE_TEMPLATE",
    31  				"issue_template.md",
    32  				"issue_template.txt",
    33  				"pull_request_template.md",
    34  				".github/issue_template.md",
    35  				"docs/issue_template.md",
    36  			},
    37  			args: args{
    38  				rootDir: tmpdir,
    39  				name:    "ISSUE_TEMPLATE",
    40  			},
    41  			want: []string{},
    42  		},
    43  		{
    44  			name: "Template folder in .github takes precedence",
    45  			prepare: []string{
    46  				"ISSUE_TEMPLATE.md",
    47  				"docs/ISSUE_TEMPLATE/abc.md",
    48  				"ISSUE_TEMPLATE/abc.md",
    49  				".github/ISSUE_TEMPLATE/abc.md",
    50  			},
    51  			args: args{
    52  				rootDir: tmpdir,
    53  				name:    "ISSUE_TEMPLATE",
    54  			},
    55  			want: []string{
    56  				path.Join(tmpdir, ".github/ISSUE_TEMPLATE/abc.md"),
    57  			},
    58  		},
    59  		{
    60  			name: "Template folder in root",
    61  			prepare: []string{
    62  				"ISSUE_TEMPLATE.md",
    63  				"docs/ISSUE_TEMPLATE/abc.md",
    64  				"ISSUE_TEMPLATE/abc.md",
    65  			},
    66  			args: args{
    67  				rootDir: tmpdir,
    68  				name:    "ISSUE_TEMPLATE",
    69  			},
    70  			want: []string{
    71  				path.Join(tmpdir, "ISSUE_TEMPLATE/abc.md"),
    72  			},
    73  		},
    74  		{
    75  			name: "Template folder in docs",
    76  			prepare: []string{
    77  				"ISSUE_TEMPLATE.md",
    78  				"docs/ISSUE_TEMPLATE/abc.md",
    79  			},
    80  			args: args{
    81  				rootDir: tmpdir,
    82  				name:    "ISSUE_TEMPLATE",
    83  			},
    84  			want: []string{
    85  				path.Join(tmpdir, "docs/ISSUE_TEMPLATE/abc.md"),
    86  			},
    87  		},
    88  		{
    89  			name: "Multiple templates in template folder",
    90  			prepare: []string{
    91  				".github/ISSUE_TEMPLATE/nope.md",
    92  				".github/PULL_REQUEST_TEMPLATE.md",
    93  				".github/PULL_REQUEST_TEMPLATE/one.md",
    94  				".github/PULL_REQUEST_TEMPLATE/two.md",
    95  				".github/PULL_REQUEST_TEMPLATE/three.md",
    96  				"docs/pull_request_template.md",
    97  			},
    98  			args: args{
    99  				rootDir: tmpdir,
   100  				name:    "PuLl_ReQuEsT_TeMpLaTe",
   101  			},
   102  			want: []string{
   103  				path.Join(tmpdir, ".github/PULL_REQUEST_TEMPLATE/one.md"),
   104  				path.Join(tmpdir, ".github/PULL_REQUEST_TEMPLATE/three.md"),
   105  				path.Join(tmpdir, ".github/PULL_REQUEST_TEMPLATE/two.md"),
   106  			},
   107  		},
   108  		{
   109  			name: "Empty template directories",
   110  			prepare: []string{
   111  				".github/ISSUE_TEMPLATE/.keep",
   112  				".docs/ISSUE_TEMPLATE/.keep",
   113  				"ISSUE_TEMPLATE/.keep",
   114  			},
   115  			args: args{
   116  				rootDir: tmpdir,
   117  				name:    "ISSUE_TEMPLATE",
   118  			},
   119  			want: []string{},
   120  		},
   121  	}
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			for _, p := range tt.prepare {
   125  				fp := path.Join(tmpdir, p)
   126  				_ = os.MkdirAll(path.Dir(fp), 0700)
   127  				file, err := os.Create(fp)
   128  				if err != nil {
   129  					t.Fatal(err)
   130  				}
   131  				file.Close()
   132  			}
   133  
   134  			if got := FindNonLegacy(tt.args.rootDir, tt.args.name); !reflect.DeepEqual(got, tt.want) {
   135  				t.Errorf("Find() = %v, want %v", got, tt.want)
   136  			}
   137  		})
   138  		os.RemoveAll(tmpdir)
   139  	}
   140  }
   141  
   142  func TestFindLegacy(t *testing.T) {
   143  	tmpdir, err := os.MkdirTemp("", "gh-cli")
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	type args struct {
   149  		rootDir string
   150  		name    string
   151  	}
   152  	tests := []struct {
   153  		name    string
   154  		prepare []string
   155  		args    args
   156  		want    string
   157  	}{
   158  		{
   159  			name: "Template in root",
   160  			prepare: []string{
   161  				"README.md",
   162  				"issue_template.md",
   163  				"issue_template.txt",
   164  				"pull_request_template.md",
   165  				"docs/issue_template.md",
   166  			},
   167  			args: args{
   168  				rootDir: tmpdir,
   169  				name:    "ISSUE_TEMPLATE",
   170  			},
   171  			want: path.Join(tmpdir, "issue_template.md"),
   172  		},
   173  		{
   174  			name: "No extension",
   175  			prepare: []string{
   176  				"README.md",
   177  				"issue_template",
   178  				"docs/issue_template.md",
   179  			},
   180  			args: args{
   181  				rootDir: tmpdir,
   182  				name:    "ISSUE_TEMPLATE",
   183  			},
   184  			want: path.Join(tmpdir, "issue_template"),
   185  		},
   186  		{
   187  			name: "Dash instead of underscore",
   188  			prepare: []string{
   189  				"README.md",
   190  				"issue-template.txt",
   191  				"docs/issue_template.md",
   192  			},
   193  			args: args{
   194  				rootDir: tmpdir,
   195  				name:    "ISSUE_TEMPLATE",
   196  			},
   197  			want: path.Join(tmpdir, "issue-template.txt"),
   198  		},
   199  		{
   200  			name: "Template in .github takes precedence",
   201  			prepare: []string{
   202  				"ISSUE_TEMPLATE.md",
   203  				".github/issue_template.md",
   204  				"docs/issue_template.md",
   205  			},
   206  			args: args{
   207  				rootDir: tmpdir,
   208  				name:    "ISSUE_TEMPLATE",
   209  			},
   210  			want: path.Join(tmpdir, ".github/issue_template.md"),
   211  		},
   212  		{
   213  			name: "Template in docs",
   214  			prepare: []string{
   215  				"README.md",
   216  				"docs/issue_template.md",
   217  			},
   218  			args: args{
   219  				rootDir: tmpdir,
   220  				name:    "ISSUE_TEMPLATE",
   221  			},
   222  			want: path.Join(tmpdir, "docs/issue_template.md"),
   223  		},
   224  		{
   225  			name: "Non legacy templates ignored",
   226  			prepare: []string{
   227  				".github/PULL_REQUEST_TEMPLATE/abc.md",
   228  				"PULL_REQUEST_TEMPLATE/abc.md",
   229  				"docs/PULL_REQUEST_TEMPLATE/abc.md",
   230  				".github/PULL_REQUEST_TEMPLATE.md",
   231  			},
   232  			args: args{
   233  				rootDir: tmpdir,
   234  				name:    "PuLl_ReQuEsT_TeMpLaTe",
   235  			},
   236  			want: path.Join(tmpdir, ".github/PULL_REQUEST_TEMPLATE.md"),
   237  		},
   238  	}
   239  	for _, tt := range tests {
   240  		t.Run(tt.name, func(t *testing.T) {
   241  			for _, p := range tt.prepare {
   242  				fp := path.Join(tmpdir, p)
   243  				_ = os.MkdirAll(path.Dir(fp), 0700)
   244  				file, err := os.Create(fp)
   245  				if err != nil {
   246  					t.Fatal(err)
   247  				}
   248  				file.Close()
   249  			}
   250  
   251  			got := FindLegacy(tt.args.rootDir, tt.args.name)
   252  			if got == "" {
   253  				t.Errorf("FindLegacy() = nil, want %v", tt.want)
   254  			} else if got != tt.want {
   255  				t.Errorf("FindLegacy() = %v, want %v", got, tt.want)
   256  			}
   257  		})
   258  		os.RemoveAll(tmpdir)
   259  	}
   260  }
   261  
   262  func TestExtractName(t *testing.T) {
   263  	tmpfile, err := os.CreateTemp(t.TempDir(), "gh-cli")
   264  	if err != nil {
   265  		t.Fatal(err)
   266  	}
   267  	defer tmpfile.Close()
   268  
   269  	type args struct {
   270  		filePath string
   271  	}
   272  	tests := []struct {
   273  		name    string
   274  		prepare string
   275  		args    args
   276  		want    string
   277  	}{
   278  		{
   279  			name: "Complete front-matter",
   280  			prepare: `---
   281  name: Bug Report
   282  about: This is how you report bugs
   283  ---
   284  
   285  **Template contents**
   286  `,
   287  			args: args{
   288  				filePath: tmpfile.Name(),
   289  			},
   290  			want: "Bug Report",
   291  		},
   292  		{
   293  			name: "Incomplete front-matter",
   294  			prepare: `---
   295  about: This is how you report bugs
   296  ---
   297  `,
   298  			args: args{
   299  				filePath: tmpfile.Name(),
   300  			},
   301  			want: path.Base(tmpfile.Name()),
   302  		},
   303  		{
   304  			name:    "No front-matter",
   305  			prepare: `name: This is not yaml!`,
   306  			args: args{
   307  				filePath: tmpfile.Name(),
   308  			},
   309  			want: path.Base(tmpfile.Name()),
   310  		},
   311  	}
   312  	for _, tt := range tests {
   313  		t.Run(tt.name, func(t *testing.T) {
   314  			_ = os.WriteFile(tmpfile.Name(), []byte(tt.prepare), 0600)
   315  			if got := ExtractName(tt.args.filePath); got != tt.want {
   316  				t.Errorf("ExtractName() = %v, want %v", got, tt.want)
   317  			}
   318  		})
   319  	}
   320  }
   321  
   322  func TestExtractContents(t *testing.T) {
   323  	tmpfile, err := os.CreateTemp(t.TempDir(), "gh-cli")
   324  	if err != nil {
   325  		t.Fatal(err)
   326  	}
   327  	defer tmpfile.Close()
   328  
   329  	type args struct {
   330  		filePath string
   331  	}
   332  	tests := []struct {
   333  		name    string
   334  		prepare string
   335  		args    args
   336  		want    string
   337  	}{
   338  		{
   339  			name: "Has front-matter",
   340  			prepare: `---
   341  name: Bug Report
   342  ---
   343  
   344  
   345  Template contents
   346  ---
   347  More of template
   348  `,
   349  			args: args{
   350  				filePath: tmpfile.Name(),
   351  			},
   352  			want: `Template contents
   353  ---
   354  More of template
   355  `,
   356  		},
   357  		{
   358  			name: "No front-matter",
   359  			prepare: `Template contents
   360  ---
   361  More of template
   362  ---
   363  Even more
   364  `,
   365  			args: args{
   366  				filePath: tmpfile.Name(),
   367  			},
   368  			want: `Template contents
   369  ---
   370  More of template
   371  ---
   372  Even more
   373  `,
   374  		},
   375  	}
   376  	for _, tt := range tests {
   377  		t.Run(tt.name, func(t *testing.T) {
   378  			_ = os.WriteFile(tmpfile.Name(), []byte(tt.prepare), 0600)
   379  			if got := ExtractContents(tt.args.filePath); string(got) != tt.want {
   380  				t.Errorf("ExtractContents() = %v, want %v", string(got), tt.want)
   381  			}
   382  		})
   383  	}
   384  }