github.com/autonomy/conform@v0.1.0-alpha.16/internal/policy/commit/commit_test.go (about)

     1  /* This Source Code Form is subject to the terms of the Mozilla Public
     2   * License, v. 2.0. If a copy of the MPL was not distributed with this
     3   * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     4  
     5  package commit
     6  
     7  import (
     8  	"io/ioutil"
     9  	"log"
    10  	"os"
    11  	"os/exec"
    12  	"testing"
    13  
    14  	"github.com/autonomy/conform/internal/policy"
    15  )
    16  
    17  func RemoveAll(dir string) {
    18  	err := os.RemoveAll(dir)
    19  	if err != nil {
    20  		log.Fatal(err)
    21  	}
    22  }
    23  
    24  func TestConventionalCommitPolicy(t *testing.T) {
    25  	type testDesc struct {
    26  		Name         string
    27  		CreateCommit func() error
    28  		ExpectValid  bool
    29  	}
    30  
    31  	for _, test := range []testDesc{
    32  		{
    33  			Name:         "Valid",
    34  			CreateCommit: createValidCommit,
    35  			ExpectValid:  true,
    36  		},
    37  		{
    38  			Name:         "Invalid",
    39  			CreateCommit: createInvalidCommit,
    40  			ExpectValid:  false,
    41  		},
    42  		{
    43  			Name:         "Empty",
    44  			CreateCommit: createEmptyCommit,
    45  			ExpectValid:  false,
    46  		},
    47  	} {
    48  		func(test testDesc) {
    49  			t.Run(test.Name, func(tt *testing.T) {
    50  				dir, err := ioutil.TempDir("", "test")
    51  				if err != nil {
    52  					log.Fatal(err)
    53  				}
    54  				defer RemoveAll(dir)
    55  				err = os.Chdir(dir)
    56  				if err != nil {
    57  					tt.Error(err)
    58  				}
    59  				err = initRepo()
    60  				if err != nil {
    61  					tt.Error(err)
    62  				}
    63  
    64  				err = test.CreateCommit()
    65  				if err != nil {
    66  					tt.Error(err)
    67  				}
    68  				report, err := runCompliance()
    69  				if err != nil {
    70  					t.Error(err)
    71  				}
    72  
    73  				if test.ExpectValid {
    74  					if !report.Valid() {
    75  						tt.Error("Report is invalid with valid conventional commit")
    76  					}
    77  				} else {
    78  					if report.Valid() {
    79  						tt.Error("Report is valid with invalid conventional commit")
    80  					}
    81  				}
    82  			})
    83  		}(test)
    84  	}
    85  }
    86  
    87  func TestValidateDCO(t *testing.T) {
    88  	type testDesc struct {
    89  		Name          string
    90  		CommitMessage string
    91  		ExpectValid   bool
    92  	}
    93  
    94  	for _, test := range []testDesc{
    95  		{
    96  			Name:          "Valid DCO",
    97  			CommitMessage: "something nice\n\nSigned-off-by: Foo Bar <foobar@example.org>\n\n",
    98  			ExpectValid:   true,
    99  		},
   100  		{
   101  			Name:          "Valid DCO with CRLF",
   102  			CommitMessage: "something nice\r\n\r\nSigned-off-by: Foo Bar <foobar@example.org>\r\n\r\n",
   103  			ExpectValid:   true,
   104  		},
   105  		{
   106  			Name:          "No DCO",
   107  			CommitMessage: "something nice\n\nnot signed\n",
   108  			ExpectValid:   false,
   109  		},
   110  	} {
   111  		// Fixes scopelint error.
   112  		test := test
   113  		t.Run(test.Name, func(tt *testing.T) {
   114  			var report policy.Report
   115  			c := Commit{msg: test.CommitMessage}
   116  			report.AddCheck(c.ValidateDCO())
   117  
   118  			if test.ExpectValid {
   119  				if !report.Valid() {
   120  					tt.Error("Report is invalid with valid DCP")
   121  				}
   122  			} else {
   123  				if report.Valid() {
   124  					tt.Error("Report is valid with invalid DCO")
   125  				}
   126  			}
   127  		})
   128  	}
   129  }
   130  
   131  func TestValidConventionalCommitPolicy(t *testing.T) {
   132  	dir, err := ioutil.TempDir("", "test")
   133  	if err != nil {
   134  		log.Fatal(err)
   135  	}
   136  	defer RemoveAll(dir)
   137  	err = os.Chdir(dir)
   138  	if err != nil {
   139  		t.Error(err)
   140  	}
   141  	err = initRepo()
   142  	if err != nil {
   143  		t.Error(err)
   144  	}
   145  	err = createValidCommit()
   146  	if err != nil {
   147  		t.Error(err)
   148  	}
   149  	report, err := runCompliance()
   150  	if err != nil {
   151  		t.Error(err)
   152  	}
   153  	if !report.Valid() {
   154  		t.Errorf("Report is invalid with valid conventional commit")
   155  	}
   156  }
   157  
   158  // nolint: dupl
   159  func TestInvalidConventionalCommitPolicy(t *testing.T) {
   160  	dir, err := ioutil.TempDir("", "test")
   161  	if err != nil {
   162  		log.Fatal(err)
   163  	}
   164  	defer RemoveAll(dir)
   165  	err = os.Chdir(dir)
   166  	if err != nil {
   167  		t.Error(err)
   168  	}
   169  	err = initRepo()
   170  	if err != nil {
   171  		t.Error(err)
   172  	}
   173  	err = createInvalidCommit()
   174  	if err != nil {
   175  		t.Error(err)
   176  	}
   177  	report, err := runCompliance()
   178  	if err != nil {
   179  		t.Error(err)
   180  	}
   181  	if report.Valid() {
   182  		t.Errorf("Report is valid with invalid conventional commit")
   183  	}
   184  }
   185  
   186  // nolint: dupl
   187  func TestEmptyConventionalCommitPolicy(t *testing.T) {
   188  	dir, err := ioutil.TempDir("", "test")
   189  	if err != nil {
   190  		log.Fatal(err)
   191  	}
   192  	defer RemoveAll(dir)
   193  	err = os.Chdir(dir)
   194  	if err != nil {
   195  		t.Error(err)
   196  	}
   197  	err = initRepo()
   198  	if err != nil {
   199  		t.Error(err)
   200  	}
   201  	err = createEmptyCommit()
   202  	if err != nil {
   203  		t.Error(err)
   204  	}
   205  	report, err := runCompliance()
   206  	if err != nil {
   207  		t.Error(err)
   208  	}
   209  	if report.Valid() {
   210  		t.Error("Report is valid with invalid conventional commit")
   211  	}
   212  }
   213  
   214  func runCompliance() (*policy.Report, error) {
   215  	c := &Commit{
   216  		Conventional: &Conventional{
   217  			Types:  []string{"type"},
   218  			Scopes: []string{"scope"},
   219  		},
   220  	}
   221  
   222  	return c.Compliance(&policy.Options{})
   223  }
   224  
   225  func initRepo() error {
   226  	_, err := exec.Command("git", "init").Output()
   227  	if err != nil {
   228  		return err
   229  	}
   230  	_, err = exec.Command("touch", "test").Output()
   231  	if err != nil {
   232  		return err
   233  	}
   234  	_, err = exec.Command("git", "add", "test").Output()
   235  
   236  	return err
   237  }
   238  
   239  func createValidCommit() error {
   240  	_, err := exec.Command("git", "-c", "user.name='test'", "-c", "user.email='test@autonomy.io'", "commit", "-m", "type(scope): description").Output()
   241  
   242  	return err
   243  }
   244  
   245  func createInvalidCommit() error {
   246  	_, err := exec.Command("git", "-c", "user.name='test'", "-c", "user.email='test@autonomy.io'", "commit", "-m", "invalid commit").Output()
   247  
   248  	return err
   249  }
   250  
   251  func createEmptyCommit() error {
   252  	_, err := exec.Command("git", "-c", "user.name='test'", "-c", "user.email='test@autonomy.io'", "commit", "--allow-empty-message", "-m", "").Output()
   253  
   254  	return err
   255  }