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