github.com/bzz/enry@v1.6.7/internal/code-generator/generator/generator_test.go (about)

     1  package generator
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/suite"
    13  )
    14  
    15  const (
    16  	linguistURL          = "https://github.com/github/linguist.git"
    17  	linguistClonedEnvVar = "ENRY_TEST_REPO"
    18  	commit               = "d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68"
    19  	samplesDir           = "samples"
    20  	languagesFile        = "lib/linguist/languages.yml"
    21  
    22  	testDir   = "test_files"
    23  	assetsDir = "../assets"
    24  
    25  	// Extensions test
    26  	extensionGold         = testDir + "/extension.gold"
    27  	extensionTestTmplPath = assetsDir + "/extension.go.tmpl"
    28  	extensionTestTmplName = "extension.go.tmpl"
    29  
    30  	// Heuristics test
    31  	heuristicsTestFile  = "lib/linguist/heuristics.rb"
    32  	contentGold         = testDir + "/content.gold"
    33  	contentTestTmplPath = assetsDir + "/content.go.tmpl"
    34  	contentTestTmplName = "content.go.tmpl"
    35  
    36  	// Vendor test
    37  	vendorTestFile     = "lib/linguist/vendor.yml"
    38  	vendorGold         = testDir + "/vendor.gold"
    39  	vendorTestTmplPath = assetsDir + "/vendor.go.tmpl"
    40  	vendorTestTmplName = "vendor.go.tmpl"
    41  
    42  	// Documentation test
    43  	documentationTestFile     = "lib/linguist/documentation.yml"
    44  	documentationGold         = testDir + "/documentation.gold"
    45  	documentationTestTmplPath = assetsDir + "/documentation.go.tmpl"
    46  	documentationTestTmplName = "documentation.go.tmpl"
    47  
    48  	// Types test
    49  	typeGold         = testDir + "/type.gold"
    50  	typeTestTmplPath = assetsDir + "/type.go.tmpl"
    51  	typeTestTmplName = "type.go.tmpl"
    52  
    53  	// Interpreters test
    54  	interpreterGold         = testDir + "/interpreter.gold"
    55  	interpreterTestTmplPath = assetsDir + "/interpreter.go.tmpl"
    56  	interpreterTestTmplName = "interpreter.go.tmpl"
    57  
    58  	// Filenames test
    59  	filenameGold         = testDir + "/filename.gold"
    60  	filenameTestTmplPath = assetsDir + "/filename.go.tmpl"
    61  	filenameTestTmplName = "filename.go.tmpl"
    62  
    63  	// Aliases test
    64  	aliasGold         = testDir + "/alias.gold"
    65  	aliasTestTmplPath = assetsDir + "/alias.go.tmpl"
    66  	aliasTestTmplName = "alias.go.tmpl"
    67  
    68  	// Frequencies test
    69  	frequenciesGold         = testDir + "/frequencies.gold"
    70  	frequenciesTestTmplPath = assetsDir + "/frequencies.go.tmpl"
    71  	frequenciesTestTmplName = "frequencies.go.tmpl"
    72  
    73  	// commit test
    74  	commitGold         = testDir + "/commit.gold"
    75  	commitTestTmplPath = assetsDir + "/commit.go.tmpl"
    76  	commitTestTmplName = "commit.go.tmpl"
    77  
    78  	// mime test
    79  	mimeTypeGold         = testDir + "/mimeType.gold"
    80  	mimeTypeTestTmplPath = assetsDir + "/mimeType.go.tmpl"
    81  	mimeTypeTestTmplName = "mimeType.go.tmpl"
    82  )
    83  
    84  type GeneratorTestSuite struct {
    85  	suite.Suite
    86  	tmpLinguist string
    87  	cloned      bool
    88  }
    89  
    90  func TestGeneratorTestSuite(t *testing.T) {
    91  	suite.Run(t, new(GeneratorTestSuite))
    92  }
    93  
    94  func (s *GeneratorTestSuite) SetupSuite() {
    95  	var err error
    96  	s.tmpLinguist = os.Getenv(linguistClonedEnvVar)
    97  	s.cloned = s.tmpLinguist == ""
    98  	if s.cloned {
    99  		s.tmpLinguist, err = ioutil.TempDir("", "linguist-")
   100  		assert.NoError(s.T(), err)
   101  		cmd := exec.Command("git", "clone", linguistURL, s.tmpLinguist)
   102  		err = cmd.Run()
   103  		assert.NoError(s.T(), err)
   104  	}
   105  
   106  	cwd, err := os.Getwd()
   107  	assert.NoError(s.T(), err)
   108  
   109  	err = os.Chdir(s.tmpLinguist)
   110  	assert.NoError(s.T(), err)
   111  
   112  	cmd := exec.Command("git", "checkout", commit)
   113  	err = cmd.Run()
   114  	assert.NoError(s.T(), err)
   115  
   116  	err = os.Chdir(cwd)
   117  	assert.NoError(s.T(), err)
   118  }
   119  
   120  func (s *GeneratorTestSuite) TearDownSuite() {
   121  	if s.cloned {
   122  		err := os.RemoveAll(s.tmpLinguist)
   123  		assert.NoError(s.T(), err)
   124  	}
   125  }
   126  
   127  func (s *GeneratorTestSuite) TestGenerationFiles() {
   128  	tests := []struct {
   129  		name        string
   130  		fileToParse string
   131  		samplesDir  string
   132  		tmplPath    string
   133  		tmplName    string
   134  		commit      string
   135  		generate    File
   136  		wantOut     string
   137  	}{
   138  		{
   139  			name:        "Extensions()",
   140  			fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
   141  			samplesDir:  "",
   142  			tmplPath:    extensionTestTmplPath,
   143  			tmplName:    extensionTestTmplName,
   144  			commit:      commit,
   145  			generate:    Extensions,
   146  			wantOut:     extensionGold,
   147  		},
   148  		{
   149  			name:        "Heuristics()",
   150  			fileToParse: filepath.Join(s.tmpLinguist, heuristicsTestFile),
   151  			samplesDir:  "",
   152  			tmplPath:    contentTestTmplPath,
   153  			tmplName:    contentTestTmplName,
   154  			commit:      commit,
   155  			generate:    Heuristics,
   156  			wantOut:     contentGold,
   157  		},
   158  		{
   159  			name:        "Vendor()",
   160  			fileToParse: filepath.Join(s.tmpLinguist, vendorTestFile),
   161  			samplesDir:  "",
   162  			tmplPath:    vendorTestTmplPath,
   163  			tmplName:    vendorTestTmplName,
   164  			commit:      commit,
   165  			generate:    Vendor,
   166  			wantOut:     vendorGold,
   167  		},
   168  		{
   169  			name:        "Documentation()",
   170  			fileToParse: filepath.Join(s.tmpLinguist, documentationTestFile),
   171  			samplesDir:  "",
   172  			tmplPath:    documentationTestTmplPath,
   173  			tmplName:    documentationTestTmplName,
   174  			commit:      commit,
   175  			generate:    Documentation,
   176  			wantOut:     documentationGold,
   177  		},
   178  		{
   179  			name:        "Types()",
   180  			fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
   181  			samplesDir:  "",
   182  			tmplPath:    typeTestTmplPath,
   183  			tmplName:    typeTestTmplName,
   184  			commit:      commit,
   185  			generate:    Types,
   186  			wantOut:     typeGold,
   187  		},
   188  		{
   189  			name:        "Interpreters()",
   190  			fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
   191  			samplesDir:  "",
   192  			tmplPath:    interpreterTestTmplPath,
   193  			tmplName:    interpreterTestTmplName,
   194  			commit:      commit,
   195  			generate:    Interpreters,
   196  			wantOut:     interpreterGold,
   197  		},
   198  		{
   199  			name:        "Filenames()",
   200  			fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
   201  			samplesDir:  filepath.Join(s.tmpLinguist, samplesDir),
   202  			tmplPath:    filenameTestTmplPath,
   203  			tmplName:    filenameTestTmplName,
   204  			commit:      commit,
   205  			generate:    Filenames,
   206  			wantOut:     filenameGold,
   207  		},
   208  		{
   209  			name:        "Aliases()",
   210  			fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
   211  			samplesDir:  "",
   212  			tmplPath:    aliasTestTmplPath,
   213  			tmplName:    aliasTestTmplName,
   214  			commit:      commit,
   215  			generate:    Aliases,
   216  			wantOut:     aliasGold,
   217  		},
   218  		{
   219  			name:       "Frequencies()",
   220  			samplesDir: filepath.Join(s.tmpLinguist, samplesDir),
   221  			tmplPath:   frequenciesTestTmplPath,
   222  			tmplName:   frequenciesTestTmplName,
   223  			commit:     commit,
   224  			generate:   Frequencies,
   225  			wantOut:    frequenciesGold,
   226  		},
   227  		{
   228  			name:       "Commit()",
   229  			samplesDir: "",
   230  			tmplPath:   commitTestTmplPath,
   231  			tmplName:   commitTestTmplName,
   232  			commit:     commit,
   233  			generate:   Commit,
   234  			wantOut:    commitGold,
   235  		},
   236  		{
   237  			name:        "MimeType()",
   238  			fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
   239  			samplesDir:  "",
   240  			tmplPath:    mimeTypeTestTmplPath,
   241  			tmplName:    mimeTypeTestTmplName,
   242  			commit:      commit,
   243  			generate:    MimeType,
   244  			wantOut:     mimeTypeGold,
   245  		},
   246  	}
   247  
   248  	for _, test := range tests {
   249  		gold, err := ioutil.ReadFile(test.wantOut)
   250  		assert.NoError(s.T(), err)
   251  
   252  		outPath, err := ioutil.TempFile("/tmp", "generator-test-")
   253  		assert.NoError(s.T(), err)
   254  		defer os.Remove(outPath.Name())
   255  		err = test.generate(test.fileToParse, test.samplesDir, outPath.Name(), test.tmplPath, test.tmplName, test.commit)
   256  		assert.NoError(s.T(), err)
   257  		out, err := ioutil.ReadFile(outPath.Name())
   258  		assert.NoError(s.T(), err)
   259  		assert.EqualValues(s.T(), gold, out, fmt.Sprintf("%v: %v, expected: %v", test.name, string(out), string(gold)))
   260  	}
   261  }