2017-04-04 11:10:35 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-05-25 10:33:26 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2017-04-04 11:10:35 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-05-25 10:33:26 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2017-04-04 11:10:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-10-26 13:37:18 +00:00
|
|
|
linguistURL = "https://github.com/github/linguist.git"
|
|
|
|
linguistClonedEnvVar = "ENRY_TEST_REPO"
|
2017-06-21 07:58:33 +00:00
|
|
|
commit = "d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68"
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir = "samples"
|
|
|
|
languagesFile = "lib/linguist/languages.yml"
|
2017-04-10 09:30:23 +00:00
|
|
|
|
2017-05-25 10:33:26 +00:00
|
|
|
// Extensions test
|
2017-06-13 11:56:07 +00:00
|
|
|
extensionGold = "test_files/extension.gold"
|
|
|
|
extensionTestTmplPath = "../assets/extension.go.tmpl"
|
|
|
|
extensionTestTmplName = "extension.go.tmpl"
|
2017-04-05 14:01:31 +00:00
|
|
|
|
|
|
|
// Heuristics test
|
2017-06-13 11:56:07 +00:00
|
|
|
heuristicsTestFile = "lib/linguist/heuristics.rb"
|
2017-04-10 09:30:23 +00:00
|
|
|
contentGold = "test_files/content.gold"
|
2017-04-20 06:12:56 +00:00
|
|
|
contentTestTmplPath = "../assets/content.go.tmpl"
|
|
|
|
contentTestTmplName = "content.go.tmpl"
|
2017-04-06 15:31:17 +00:00
|
|
|
|
|
|
|
// Vendor test
|
2017-06-13 11:56:07 +00:00
|
|
|
vendorTestFile = "lib/linguist/vendor.yml"
|
2017-04-07 07:25:49 +00:00
|
|
|
vendorGold = "test_files/vendor.gold"
|
2017-04-20 06:12:56 +00:00
|
|
|
vendorTestTmplPath = "../assets/vendor.go.tmpl"
|
|
|
|
vendorTestTmplName = "vendor.go.tmpl"
|
2017-04-10 09:30:23 +00:00
|
|
|
|
|
|
|
// Documentation test
|
2017-06-13 11:56:07 +00:00
|
|
|
documentationTestFile = "lib/linguist/documentation.yml"
|
2017-04-10 09:30:23 +00:00
|
|
|
documentationGold = "test_files/documentation.gold"
|
2017-04-20 06:12:56 +00:00
|
|
|
documentationTestTmplPath = "../assets/documentation.go.tmpl"
|
|
|
|
documentationTestTmplName = "documentation.go.tmpl"
|
2017-04-11 09:26:23 +00:00
|
|
|
|
|
|
|
// Types test
|
2017-06-13 11:56:07 +00:00
|
|
|
typeGold = "test_files/type.gold"
|
|
|
|
typeTestTmplPath = "../assets/type.go.tmpl"
|
|
|
|
typeTestTmplName = "type.go.tmpl"
|
2017-04-17 06:19:53 +00:00
|
|
|
|
|
|
|
// Interpreters test
|
2017-06-13 11:56:07 +00:00
|
|
|
interpreterGold = "test_files/interpreter.gold"
|
|
|
|
interpreterTestTmplPath = "../assets/interpreter.go.tmpl"
|
|
|
|
interpreterTestTmplName = "interpreter.go.tmpl"
|
2017-04-19 08:22:46 +00:00
|
|
|
|
|
|
|
// Filenames test
|
2017-06-13 11:56:07 +00:00
|
|
|
filenameGold = "test_files/filename.gold"
|
|
|
|
filenameTestTmplPath = "../assets/filename.go.tmpl"
|
|
|
|
filenameTestTmplName = "filename.go.tmpl"
|
2017-05-04 13:03:54 +00:00
|
|
|
|
|
|
|
// Aliases test
|
2017-06-13 11:56:07 +00:00
|
|
|
aliasGold = "test_files/alias.gold"
|
|
|
|
aliasTestTmplPath = "../assets/alias.go.tmpl"
|
|
|
|
aliasTestTmplName = "alias.go.tmpl"
|
2017-05-25 10:33:26 +00:00
|
|
|
|
|
|
|
// Frequencies test
|
|
|
|
frequenciesGold = "test_files/frequencies.gold"
|
|
|
|
frequenciesTestTmplPath = "../assets/frequencies.go.tmpl"
|
|
|
|
frequenciesTestTmplName = "frequencies.go.tmpl"
|
2017-06-21 13:18:27 +00:00
|
|
|
|
|
|
|
// commit test
|
|
|
|
commitGold = "test_files/commit.gold"
|
|
|
|
commitTestTmplPath = "../assets/commit.go.tmpl"
|
|
|
|
commitTestTmplName = "commit.go.tmpl"
|
2017-07-11 09:13:49 +00:00
|
|
|
|
|
|
|
// mime test
|
|
|
|
mimeTypeGold = "test_files/mimeType.gold"
|
|
|
|
mimeTypeTestTmplPath = "../assets/mimeType.go.tmpl"
|
|
|
|
mimeTypeTestTmplName = "mimeType.go.tmpl"
|
2017-04-04 11:10:35 +00:00
|
|
|
)
|
|
|
|
|
2017-05-25 10:33:26 +00:00
|
|
|
type GeneratorTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
tmpLinguist string
|
2017-09-28 18:58:13 +00:00
|
|
|
cloned bool
|
2017-05-25 10:33:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 08:05:16 +00:00
|
|
|
func TestGeneratorTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(GeneratorTestSuite))
|
|
|
|
}
|
|
|
|
|
2017-06-21 13:18:27 +00:00
|
|
|
func (s *GeneratorTestSuite) SetupSuite() {
|
|
|
|
var err error
|
2017-10-26 13:37:18 +00:00
|
|
|
s.tmpLinguist = os.Getenv(linguistClonedEnvVar)
|
|
|
|
s.cloned = s.tmpLinguist == ""
|
2017-09-28 18:58:13 +00:00
|
|
|
if s.cloned {
|
|
|
|
s.tmpLinguist, err = ioutil.TempDir("", "linguist-")
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
cmd := exec.Command("git", "clone", linguistURL, s.tmpLinguist)
|
|
|
|
err = cmd.Run()
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
}
|
2017-05-25 10:33:26 +00:00
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
|
2017-06-21 13:18:27 +00:00
|
|
|
err = os.Chdir(s.tmpLinguist)
|
|
|
|
assert.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
|
2017-09-28 18:58:13 +00:00
|
|
|
cmd := exec.Command("git", "checkout", commit)
|
2017-05-25 10:33:26 +00:00
|
|
|
err = cmd.Run()
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
|
|
|
|
err = os.Chdir(cwd)
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 13:18:27 +00:00
|
|
|
func (s *GeneratorTestSuite) TearDownSuite() {
|
2017-09-28 18:58:13 +00:00
|
|
|
if s.cloned {
|
|
|
|
err := os.RemoveAll(s.tmpLinguist)
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
}
|
2017-05-25 10:33:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 13:18:27 +00:00
|
|
|
func (s *GeneratorTestSuite) TestGenerationFiles() {
|
2017-04-04 11:10:35 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fileToParse string
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir string
|
2017-04-04 11:10:35 +00:00
|
|
|
tmplPath string
|
|
|
|
tmplName string
|
|
|
|
commit string
|
2017-06-13 11:56:07 +00:00
|
|
|
generate File
|
2017-04-19 08:22:46 +00:00
|
|
|
wantOut string
|
2017-04-04 11:10:35 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Extensions()",
|
2017-06-21 13:18:27 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: extensionTestTmplPath,
|
|
|
|
tmplName: extensionTestTmplName,
|
|
|
|
commit: commit,
|
2017-04-19 09:24:01 +00:00
|
|
|
generate: Extensions,
|
2017-06-13 11:56:07 +00:00
|
|
|
wantOut: extensionGold,
|
2017-04-04 11:10:35 +00:00
|
|
|
},
|
2017-04-05 14:01:31 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Heuristics()",
|
2017-06-21 13:18:27 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguist, heuristicsTestFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
2017-04-05 14:01:31 +00:00
|
|
|
tmplPath: contentTestTmplPath,
|
|
|
|
tmplName: contentTestTmplName,
|
2017-06-13 11:56:07 +00:00
|
|
|
commit: commit,
|
2017-04-05 14:01:31 +00:00
|
|
|
generate: Heuristics,
|
2017-04-19 08:22:46 +00:00
|
|
|
wantOut: contentGold,
|
2017-04-05 14:01:31 +00:00
|
|
|
},
|
2017-04-06 15:31:17 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Vendor()",
|
2017-06-21 13:18:27 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguist, vendorTestFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
2017-04-07 07:25:49 +00:00
|
|
|
tmplPath: vendorTestTmplPath,
|
|
|
|
tmplName: vendorTestTmplName,
|
2017-06-13 11:56:07 +00:00
|
|
|
commit: commit,
|
2017-04-06 15:31:17 +00:00
|
|
|
generate: Vendor,
|
2017-04-19 08:22:46 +00:00
|
|
|
wantOut: vendorGold,
|
2017-04-06 15:31:17 +00:00
|
|
|
},
|
2017-04-10 09:30:23 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Documentation()",
|
2017-06-21 13:18:27 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguist, documentationTestFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
2017-04-10 09:30:23 +00:00
|
|
|
tmplPath: documentationTestTmplPath,
|
|
|
|
tmplName: documentationTestTmplName,
|
2017-06-13 11:56:07 +00:00
|
|
|
commit: commit,
|
2017-04-10 09:30:23 +00:00
|
|
|
generate: Documentation,
|
2017-04-19 08:22:46 +00:00
|
|
|
wantOut: documentationGold,
|
2017-04-10 09:30:23 +00:00
|
|
|
},
|
2017-04-11 09:26:23 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Types()",
|
2017-06-21 13:18:27 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: typeTestTmplPath,
|
|
|
|
tmplName: typeTestTmplName,
|
|
|
|
commit: commit,
|
2017-04-11 09:26:23 +00:00
|
|
|
generate: Types,
|
2017-06-13 11:56:07 +00:00
|
|
|
wantOut: typeGold,
|
2017-04-11 09:26:23 +00:00
|
|
|
},
|
2017-04-17 06:19:53 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Interpreters()",
|
2017-06-21 13:18:27 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: interpreterTestTmplPath,
|
|
|
|
tmplName: interpreterTestTmplName,
|
|
|
|
commit: commit,
|
2017-04-17 06:19:53 +00:00
|
|
|
generate: Interpreters,
|
2017-06-13 11:56:07 +00:00
|
|
|
wantOut: interpreterGold,
|
2017-04-19 08:22:46 +00:00
|
|
|
},
|
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Filenames()",
|
2017-06-21 13:18:27 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
|
|
|
|
samplesDir: filepath.Join(s.tmpLinguist, samplesDir),
|
2017-06-13 11:56:07 +00:00
|
|
|
tmplPath: filenameTestTmplPath,
|
|
|
|
tmplName: filenameTestTmplName,
|
|
|
|
commit: commit,
|
2017-04-19 08:22:46 +00:00
|
|
|
generate: Filenames,
|
2017-06-13 11:56:07 +00:00
|
|
|
wantOut: filenameGold,
|
2017-04-17 06:19:53 +00:00
|
|
|
},
|
2017-05-04 13:03:54 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Aliases()",
|
2017-06-21 13:18:27 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: aliasTestTmplPath,
|
|
|
|
tmplName: aliasTestTmplName,
|
|
|
|
commit: commit,
|
2017-05-04 13:03:54 +00:00
|
|
|
generate: Aliases,
|
2017-06-13 11:56:07 +00:00
|
|
|
wantOut: aliasGold,
|
2017-05-04 13:03:54 +00:00
|
|
|
},
|
2017-05-25 10:33:26 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Frequencies()",
|
2017-06-21 13:18:27 +00:00
|
|
|
samplesDir: filepath.Join(s.tmpLinguist, samplesDir),
|
2017-05-25 10:33:26 +00:00
|
|
|
tmplPath: frequenciesTestTmplPath,
|
|
|
|
tmplName: frequenciesTestTmplName,
|
2017-06-13 11:56:07 +00:00
|
|
|
commit: commit,
|
|
|
|
generate: Frequencies,
|
2017-05-25 10:33:26 +00:00
|
|
|
wantOut: frequenciesGold,
|
|
|
|
},
|
2017-06-21 13:18:27 +00:00
|
|
|
{
|
|
|
|
name: "Commit()",
|
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: commitTestTmplPath,
|
|
|
|
tmplName: commitTestTmplName,
|
|
|
|
commit: commit,
|
|
|
|
generate: Commit,
|
|
|
|
wantOut: commitGold,
|
|
|
|
},
|
2017-07-11 09:13:49 +00:00
|
|
|
{
|
|
|
|
name: "MimeType()",
|
|
|
|
fileToParse: filepath.Join(s.tmpLinguist, languagesFile),
|
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: mimeTypeTestTmplPath,
|
|
|
|
tmplName: mimeTypeTestTmplName,
|
|
|
|
commit: commit,
|
|
|
|
generate: MimeType,
|
|
|
|
wantOut: mimeTypeGold,
|
|
|
|
},
|
2017-05-25 10:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
gold, err := ioutil.ReadFile(test.wantOut)
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
|
2017-06-13 11:56:07 +00:00
|
|
|
outPath, err := ioutil.TempFile("/tmp", "generator-test-")
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
defer os.Remove(outPath.Name())
|
2017-06-13 11:56:07 +00:00
|
|
|
err = test.generate(test.fileToParse, test.samplesDir, outPath.Name(), test.tmplPath, test.tmplName, test.commit)
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
out, err := ioutil.ReadFile(outPath.Name())
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
assert.EqualValues(s.T(), gold, out, fmt.Sprintf("%v: %v, expected: %v", test.name, string(out), string(gold)))
|
2017-05-25 10:33:26 +00:00
|
|
|
}
|
|
|
|
}
|