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-05-25 10:33:26 +00:00
|
|
|
lingustURL = "https://github.com/github/linguist.git"
|
|
|
|
commitTree = "60f864a138650dd17fafc94814be9ee2d3aaef8c"
|
2017-05-29 08:05:16 +00:00
|
|
|
commitTest = "0123456789abcdef0123456789abcdef01234567"
|
2017-04-10 09:30:23 +00:00
|
|
|
|
2017-05-25 10:33:26 +00:00
|
|
|
// Extensions test
|
2017-04-19 09:24:01 +00:00
|
|
|
extensionsTestFile = "test_files/extensions.test.yml"
|
|
|
|
extensionsGold = "test_files/extensions.gold"
|
2017-04-20 06:12:56 +00:00
|
|
|
extensionsTestTmplPath = "../assets/extensions.go.tmpl"
|
|
|
|
extensionsTestTmplName = "extensions.go.tmpl"
|
2017-04-05 14:01:31 +00:00
|
|
|
|
|
|
|
// Heuristics test
|
2017-04-10 09:30:23 +00:00
|
|
|
heuristicsTestFile = "test_files/heuristics.test.rb"
|
|
|
|
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-04-07 07:25:49 +00:00
|
|
|
vendorTestFile = "test_files/vendor.test.yml"
|
|
|
|
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
|
|
|
|
documentationTestFile = "test_files/documentation.test.yml"
|
|
|
|
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-05-03 10:17:54 +00:00
|
|
|
typesTestFile = "test_files/types.test.yml"
|
|
|
|
typesGold = "test_files/types.gold"
|
|
|
|
typesTestTmplPath = "../assets/types.go.tmpl"
|
|
|
|
typesTestTmplName = "types.go.tmpl"
|
2017-04-17 06:19:53 +00:00
|
|
|
|
|
|
|
// Interpreters test
|
|
|
|
interpretersTestFile = "test_files/interpreters.test.yml"
|
|
|
|
interpretersGold = "test_files/interpreters.gold"
|
2017-04-20 06:12:56 +00:00
|
|
|
interpretersTestTmplPath = "../assets/interpreters.go.tmpl"
|
|
|
|
interpretersTestTmplName = "interpreters.go.tmpl"
|
2017-04-19 08:22:46 +00:00
|
|
|
|
|
|
|
// Filenames test
|
|
|
|
filenamesTestFile = "test_files/filenames.test.yml"
|
|
|
|
filenamesGold = "test_files/filenames.gold"
|
2017-04-20 06:12:56 +00:00
|
|
|
filenamesTestTmplPath = "../assets/filenames.go.tmpl"
|
|
|
|
filenamesTestTmplName = "filenames.go.tmpl"
|
2017-05-04 13:03:54 +00:00
|
|
|
|
|
|
|
// Aliases test
|
|
|
|
aliasesTestFile = "test_files/aliases.test.yml"
|
|
|
|
aliasesGold = "test_files/aliases.gold"
|
|
|
|
aliasesTestTmplPath = "../assets/aliases.go.tmpl"
|
|
|
|
aliasesTestTmplName = "aliases.go.tmpl"
|
2017-05-25 10:33:26 +00:00
|
|
|
|
|
|
|
// Frequencies test
|
|
|
|
frequenciesTestDir = "/samples"
|
|
|
|
frequenciesGold = "test_files/frequencies.gold"
|
|
|
|
frequenciesTestTmplPath = "../assets/frequencies.go.tmpl"
|
|
|
|
frequenciesTestTmplName = "frequencies.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-05-29 08:05:16 +00:00
|
|
|
func TestGeneratorTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(GeneratorTestSuite))
|
|
|
|
}
|
|
|
|
|
2017-05-25 10:33:26 +00:00
|
|
|
func (g *GeneratorTestSuite) SetupSuite() {
|
|
|
|
tmpLinguist, err := ioutil.TempDir("", "linguist-")
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
g.tmpLinguist = tmpLinguist
|
|
|
|
|
|
|
|
cmd := exec.Command("git", "clone", lingustURL, tmpLinguist)
|
|
|
|
err = cmd.Run()
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
|
|
|
|
err = os.Chdir(tmpLinguist)
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
|
|
|
|
cmd = exec.Command("git", "checkout", commitTree)
|
|
|
|
err = cmd.Run()
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
|
|
|
|
err = os.Chdir(cwd)
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GeneratorTestSuite) TearDownSuite() {
|
|
|
|
err := os.RemoveAll(g.tmpLinguist)
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GeneratorTestSuite) TestFromFile() {
|
2017-04-04 11:10:35 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fileToParse string
|
|
|
|
tmplPath string
|
|
|
|
tmplName string
|
|
|
|
commit string
|
|
|
|
generate Func
|
2017-04-19 08:22:46 +00:00
|
|
|
wantOut string
|
2017-04-04 11:10:35 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-05-29 08:05:16 +00:00
|
|
|
name: "TestFromFile_Extensions",
|
2017-04-19 09:24:01 +00:00
|
|
|
fileToParse: extensionsTestFile,
|
|
|
|
tmplPath: extensionsTestTmplPath,
|
|
|
|
tmplName: extensionsTestTmplName,
|
2017-04-10 09:30:23 +00:00
|
|
|
commit: commitTest,
|
2017-04-19 09:24:01 +00:00
|
|
|
generate: Extensions,
|
|
|
|
wantOut: extensionsGold,
|
2017-04-04 11:10:35 +00:00
|
|
|
},
|
2017-04-05 14:01:31 +00:00
|
|
|
{
|
|
|
|
name: "TestFromFile_Heuristics",
|
|
|
|
fileToParse: heuristicsTestFile,
|
|
|
|
tmplPath: contentTestTmplPath,
|
|
|
|
tmplName: contentTestTmplName,
|
2017-04-10 09:30:23 +00:00
|
|
|
commit: commitTest,
|
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
|
|
|
{
|
|
|
|
name: "TestFromFile_Vendor",
|
|
|
|
fileToParse: vendorTestFile,
|
2017-04-07 07:25:49 +00:00
|
|
|
tmplPath: vendorTestTmplPath,
|
|
|
|
tmplName: vendorTestTmplName,
|
2017-04-10 09:30:23 +00:00
|
|
|
commit: commitTest,
|
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
|
|
|
{
|
|
|
|
name: "TestFromFile_Documentation",
|
|
|
|
fileToParse: documentationTestFile,
|
|
|
|
tmplPath: documentationTestTmplPath,
|
|
|
|
tmplName: documentationTestTmplName,
|
|
|
|
commit: commitTest,
|
|
|
|
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-04-17 06:14:46 +00:00
|
|
|
name: "TestFromFile_Types",
|
2017-04-11 09:26:23 +00:00
|
|
|
fileToParse: typesTestFile,
|
|
|
|
tmplPath: typesTestTmplPath,
|
|
|
|
tmplName: typesTestTmplName,
|
|
|
|
commit: commitTest,
|
|
|
|
generate: Types,
|
2017-04-19 08:22:46 +00:00
|
|
|
wantOut: typesGold,
|
2017-04-11 09:26:23 +00:00
|
|
|
},
|
2017-04-17 06:19:53 +00:00
|
|
|
{
|
|
|
|
name: "TestFromFile_Interpreters",
|
|
|
|
fileToParse: interpretersTestFile,
|
|
|
|
tmplPath: interpretersTestTmplPath,
|
|
|
|
tmplName: interpretersTestTmplName,
|
|
|
|
commit: commitTest,
|
|
|
|
generate: Interpreters,
|
2017-04-19 08:22:46 +00:00
|
|
|
wantOut: interpretersGold,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "TestFromFile_Filenames",
|
|
|
|
fileToParse: filenamesTestFile,
|
|
|
|
tmplPath: filenamesTestTmplPath,
|
|
|
|
tmplName: filenamesTestTmplName,
|
|
|
|
commit: commitTest,
|
|
|
|
generate: Filenames,
|
|
|
|
wantOut: filenamesGold,
|
2017-04-17 06:19:53 +00:00
|
|
|
},
|
2017-05-04 13:03:54 +00:00
|
|
|
{
|
|
|
|
name: "TestFromFile_Aliases",
|
|
|
|
fileToParse: aliasesTestFile,
|
|
|
|
tmplPath: aliasesTestTmplPath,
|
|
|
|
tmplName: aliasesTestTmplName,
|
|
|
|
commit: commitTest,
|
|
|
|
generate: Aliases,
|
|
|
|
wantOut: aliasesGold,
|
|
|
|
},
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 10:33:26 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
gold, err := ioutil.ReadFile(test.wantOut)
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
|
|
|
|
outPath, err := ioutil.TempFile("/tmp", "generator-test-")
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
defer os.Remove(outPath.Name())
|
|
|
|
|
|
|
|
err = FromFile(test.fileToParse, outPath.Name(), test.tmplPath, test.tmplName, test.commit, test.generate)
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
out, err := ioutil.ReadFile(outPath.Name())
|
|
|
|
assert.NoError(g.T(), err)
|
2017-05-29 08:05:16 +00:00
|
|
|
assert.EqualValues(g.T(), gold, out, fmt.Sprintf("%v: %v, expected: %v", test.name, string(out), string(test.wantOut)))
|
2017-04-04 11:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 10:33:26 +00:00
|
|
|
|
|
|
|
func (g *GeneratorTestSuite) TestFrequencies() {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
samplesDir string
|
|
|
|
tmplPath string
|
|
|
|
tmplName string
|
|
|
|
commit string
|
|
|
|
wantOut string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Frequencies_1",
|
|
|
|
samplesDir: filepath.Join(g.tmpLinguist, frequenciesTestDir),
|
|
|
|
tmplPath: frequenciesTestTmplPath,
|
|
|
|
tmplName: frequenciesTestTmplName,
|
|
|
|
commit: commitTree,
|
|
|
|
wantOut: frequenciesGold,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
gold, err := ioutil.ReadFile(test.wantOut)
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
|
|
|
|
outPath, err := ioutil.TempFile("/tmp", "frequencies-test-")
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
defer os.Remove(outPath.Name())
|
|
|
|
|
|
|
|
err = Frequencies(test.samplesDir, test.tmplPath, test.tmplName, test.commit, outPath.Name())
|
|
|
|
assert.NoError(g.T(), err)
|
|
|
|
out, err := ioutil.ReadFile(outPath.Name())
|
|
|
|
assert.NoError(g.T(), err)
|
2017-05-29 08:05:16 +00:00
|
|
|
assert.EqualValues(g.T(), gold, out, fmt.Sprintf("%v: %v, expected: %v", test.name, string(out), string(test.wantOut)))
|
2017-05-25 10:33:26 +00:00
|
|
|
}
|
|
|
|
}
|