Merge pull request #76 from mcarmonaa/fix/makefile-ci

Included src-d/ci/Makefile.main in Makefile
This commit is contained in:
Alfredo Beaumont 2017-07-18 13:48:31 +02:00 committed by GitHub
commit c885af7834
5 changed files with 99 additions and 53 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
.linguist .linguist
benchmarks/output benchmarks/output
.ci
Makefile.main

View File

@ -1,32 +1,36 @@
COVERAGE_REPORT := coverage.txt # Package configuration
COVERAGE_PROFILE := profile.out PROJECT = enry
COVERAGE_MODE := atomic COMMANDS = cli/enry
# Including ci Makefile
MAKEFILE = Makefile.main
CI_REPOSITORY = https://github.com/src-d/ci.git
CI_FOLDER = .ci
# If you need to build more than one dockerfile, you can do so like this:
# DOCKERFILES = Dockerfile_filename1:repositoryname1 Dockerfile_filename2:repositoryname2 ...
$(MAKEFILE):
@git clone --quiet $(CI_REPOSITORY) $(CI_FOLDER); \
cp $(CI_FOLDER)/$(MAKEFILE) .;
-include $(MAKEFILE)
LINGUIST_PATH = .linguist LINGUIST_PATH = .linguist
# build CLI # build CLI
VERSION := $(shell git describe --tags --abbrev=0) LOCAL_TAG := $(shell git describe --tags --abbrev=0)
COMMIT := $(shell git rev-parse --short HEAD) LOCAL_COMMIT := $(shell git rev-parse --short HEAD)
LDFLAGS = -s -X main.Version=$(VERSION) -X main.GitHash=$(COMMIT) LOCAL_BUILD := $(shell date +"%m-%d-%Y_%H_%M_%S")
LOCAL_LDFLAGS = -s -X main.version=$(LOCAL_TAG) -X main.build=$(LOCAL_BUILD) -X main.commit=$(LOCAL_COMMIT)
$(LINGUIST_PATH): $(LINGUIST_PATH):
git clone https://github.com/github/linguist.git $@ git clone https://github.com/github/linguist.git $@
test: $(LINGUIST_PATH) clean-linguist:
go test -v ./... rm -rf $(LINGUIST_PATH)
test-coverage: $(LINGUIST_PATH) clean: clean-linguist
@echo "mode: $(COVERAGE_MODE)" > $(COVERAGE_REPORT); \
for dir in `find . -name "*.go" | grep -o '.*/' | sort -u | grep -v './fixtures/' | grep -v './.linguist/'`; do \
go test $$dir -coverprofile=$(COVERAGE_PROFILE) -covermode=$(COVERAGE_MODE); \
if [ $$? != 0 ]; then \
exit 2; \
fi; \
if [ -f $(COVERAGE_PROFILE) ]; then \
tail -n +2 $(COVERAGE_PROFILE) >> $(COVERAGE_REPORT); \
rm $(COVERAGE_PROFILE); \
fi; \
done;
code-generate: $(LINGUIST_PATH) code-generate: $(LINGUIST_PATH)
mkdir -p data mkdir -p data
@ -42,8 +46,5 @@ benchmarks-slow: $(LINGUST_PATH)
mkdir -p benchmarks/output && go test -run=NONE -bench=. -slow -benchtime=100ms -timeout=100h >benchmarks/output/enry_samples.bench && \ mkdir -p benchmarks/output && go test -run=NONE -bench=. -slow -benchtime=100ms -timeout=100h >benchmarks/output/enry_samples.bench && \
benchmarks/linguist-samples.rb 5 >benchmarks/output/linguist_samples.bench benchmarks/linguist-samples.rb 5 >benchmarks/output/linguist_samples.bench
clean:
rm -rf $(LINGUIST_PATH)
build-cli: build-cli:
go build -o enry -ldflags "$(LDFLAGS)" cli/enry/main.go go build -o enry -ldflags "$(LOCAL_LDFLAGS)" cli/enry/main.go

View File

@ -5,11 +5,12 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"testing" "testing"
)
const samplesDir = ".linguist/samples" "gopkg.in/src-d/enry.v1/data"
)
type sample struct { type sample struct {
filename string filename string
@ -21,18 +22,62 @@ var (
overcomeLanguage string overcomeLanguage string
overcomeLanguages []string overcomeLanguages []string
samples []*sample samples []*sample
samplesDir string
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
var exitCode int
defer os.Exit(exitCode)
flag.BoolVar(&slow, "slow", false, "run benchmarks per sample for strategies too") flag.BoolVar(&slow, "slow", false, "run benchmarks per sample for strategies too")
flag.Parse() flag.Parse()
if err := cloneLinguist(linguistURL); err != nil {
log.Fatal(err)
}
defer os.RemoveAll(filepath.Dir(samplesDir))
var err error var err error
samples, err = getSamples(samplesDir) samples, err = getSamples(samplesDir)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
os.Exit(m.Run()) exitCode = m.Run()
}
func cloneLinguist(linguistURL string) error {
repoLinguist, err := ioutil.TempDir("", "linguist-")
if err != nil {
return err
}
samplesDir = filepath.Join(repoLinguist, "samples")
cmd := exec.Command("git", "clone", linguistURL, repoLinguist)
if err := cmd.Run(); err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return err
}
if err = os.Chdir(repoLinguist); err != nil {
return err
}
cmd = exec.Command("git", "checkout", data.LinguistCommit)
if err := cmd.Run(); err != nil {
return err
}
if err = os.Chdir(cwd); err != nil {
return err
}
return nil
} }
func getSamples(dir string) ([]*sample, error) { func getSamples(dir string) ([]*sample, error) {

View File

@ -14,8 +14,9 @@ import (
) )
var ( var (
Version = "undefined" version = "undefined"
GitHash = "undefined" build = "undefined"
commit = "undefined"
) )
func main() { func main() {
@ -111,13 +112,13 @@ func main() {
func usage() { func usage() {
fmt.Fprintf( fmt.Fprintf(
os.Stderr, os.Stderr,
` %[1]s %[2]s commit: %[3]s ` %[1]s %[2]s build: %[3]s commit: %[4]s
enry, A simple (and faster) implementation of github/linguist %[1]s, A simple (and faster) implementation of github/linguist
usage: %[1]s <path> usage: %[1]s <path>
%[1]s [-json] [-breakdown] <path> %[1]s [-json] [-breakdown] <path>
%[1]s [-json] [-breakdown] %[1]s [-json] [-breakdown]
`, `,
os.Args[0], Version, GitHash, os.Args[0], version, build, commit,
) )
} }

View File

@ -19,6 +19,7 @@ const linguistURL = "https://github.com/github/linguist.git"
type EnryTestSuite struct { type EnryTestSuite struct {
suite.Suite suite.Suite
repoLinguist string repoLinguist string
samplesDir string
} }
func TestEnryTestSuite(t *testing.T) { func TestEnryTestSuite(t *testing.T) {
@ -30,6 +31,8 @@ func (s *EnryTestSuite) SetupSuite() {
s.repoLinguist, err = ioutil.TempDir("", "linguist-") s.repoLinguist, err = ioutil.TempDir("", "linguist-")
assert.NoError(s.T(), err) assert.NoError(s.T(), err)
s.samplesDir = filepath.Join(s.repoLinguist, "samples")
cmd := exec.Command("git", "clone", linguistURL, s.repoLinguist) cmd := exec.Command("git", "clone", linguistURL, s.repoLinguist)
err = cmd.Run() err = cmd.Run()
assert.NoError(s.T(), err) assert.NoError(s.T(), err)
@ -74,10 +77,7 @@ func (s *EnryTestSuite) TestGetLanguage() {
} }
func (s *EnryTestSuite) TestGetLanguagesByModelineLinguist() { func (s *EnryTestSuite) TestGetLanguagesByModelineLinguist() {
const ( var modelinesDir = filepath.Join(s.repoLinguist, "test/fixtures/Data/Modelines")
modelinesDir = ".linguist/test/fixtures/Data/Modelines"
samplesDir = ".linguist/samples"
)
tests := []struct { tests := []struct {
name string name string
@ -118,7 +118,7 @@ func (s *EnryTestSuite) TestGetLanguagesByModelineLinguist() {
{name: "TestGetLanguagesByModelineLinguist_28", filename: filepath.Join(modelinesDir, "ruby10"), expected: []string{"Ruby"}}, {name: "TestGetLanguagesByModelineLinguist_28", filename: filepath.Join(modelinesDir, "ruby10"), expected: []string{"Ruby"}},
{name: "TestGetLanguagesByModelineLinguist_29", filename: filepath.Join(modelinesDir, "ruby11"), expected: []string{"Ruby"}}, {name: "TestGetLanguagesByModelineLinguist_29", filename: filepath.Join(modelinesDir, "ruby11"), expected: []string{"Ruby"}},
{name: "TestGetLanguagesByModelineLinguist_30", filename: filepath.Join(modelinesDir, "ruby12"), expected: []string{"Ruby"}}, {name: "TestGetLanguagesByModelineLinguist_30", filename: filepath.Join(modelinesDir, "ruby12"), expected: []string{"Ruby"}},
{name: "TestGetLanguagesByModelineLinguist_31", filename: filepath.Join(samplesDir, "C/main.c"), expected: nil}, {name: "TestGetLanguagesByModelineLinguist_31", filename: filepath.Join(s.samplesDir, "C/main.c"), expected: nil},
} }
for _, test := range tests { for _, test := range tests {
@ -240,19 +240,18 @@ func (s *EnryTestSuite) TestGetLanguagesByExtension() {
} }
func (s *EnryTestSuite) TestGetLanguagesByClassifier() { func (s *EnryTestSuite) TestGetLanguagesByClassifier() {
const samples = `.linguist/samples/`
test := []struct { test := []struct {
name string name string
filename string filename string
candidates []string candidates []string
expected string expected string
}{ }{
{name: "TestGetLanguagesByClassifier_1", filename: filepath.Join(samples, "C/blob.c"), candidates: []string{"python", "ruby", "c", "c++"}, expected: "C"}, {name: "TestGetLanguagesByClassifier_1", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: []string{"python", "ruby", "c", "c++"}, expected: "C"},
{name: "TestGetLanguagesByClassifier_2", filename: filepath.Join(samples, "C/blob.c"), candidates: nil, expected: OtherLanguage}, {name: "TestGetLanguagesByClassifier_2", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: nil, expected: OtherLanguage},
{name: "TestGetLanguagesByClassifier_3", filename: filepath.Join(samples, "C/main.c"), candidates: []string{}, expected: OtherLanguage}, {name: "TestGetLanguagesByClassifier_3", filename: filepath.Join(s.samplesDir, "C/main.c"), candidates: []string{}, expected: OtherLanguage},
{name: "TestGetLanguagesByClassifier_4", filename: filepath.Join(samples, "C/blob.c"), candidates: []string{"python", "ruby", "c++"}, expected: "C++"}, {name: "TestGetLanguagesByClassifier_4", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: []string{"python", "ruby", "c++"}, expected: "C++"},
{name: "TestGetLanguagesByClassifier_5", filename: filepath.Join(samples, "C/blob.c"), candidates: []string{"ruby"}, expected: "Ruby"}, {name: "TestGetLanguagesByClassifier_5", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: []string{"ruby"}, expected: "Ruby"},
{name: "TestGetLanguagesByClassifier_6", filename: filepath.Join(samples, "Python/django-models-base.py"), candidates: []string{"python", "ruby", "c", "c++"}, expected: "Python"}, {name: "TestGetLanguagesByClassifier_6", filename: filepath.Join(s.samplesDir, "Python/django-models-base.py"), candidates: []string{"python", "ruby", "c", "c++"}, expected: "Python"},
} }
for _, test := range test { for _, test := range test {
@ -272,7 +271,6 @@ func (s *EnryTestSuite) TestGetLanguagesByClassifier() {
} }
func (s *EnryTestSuite) TestGetLanguagesBySpecificClassifier() { func (s *EnryTestSuite) TestGetLanguagesBySpecificClassifier() {
const samples = `.linguist/samples/`
test := []struct { test := []struct {
name string name string
filename string filename string
@ -280,12 +278,12 @@ func (s *EnryTestSuite) TestGetLanguagesBySpecificClassifier() {
classifier Classifier classifier Classifier
expected string expected string
}{ }{
{name: "TestGetLanguagesByClassifier_1", filename: filepath.Join(samples, "C/blob.c"), candidates: []string{"python", "ruby", "c", "c++"}, classifier: DefaultClassifier, expected: "C"}, {name: "TestGetLanguagesByClassifier_1", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: []string{"python", "ruby", "c", "c++"}, classifier: DefaultClassifier, expected: "C"},
{name: "TestGetLanguagesByClassifier_2", filename: filepath.Join(samples, "C/blob.c"), candidates: nil, classifier: DefaultClassifier, expected: "C"}, {name: "TestGetLanguagesByClassifier_2", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: nil, classifier: DefaultClassifier, expected: "C"},
{name: "TestGetLanguagesByClassifier_3", filename: filepath.Join(samples, "C/main.c"), candidates: []string{}, classifier: DefaultClassifier, expected: "C"}, {name: "TestGetLanguagesByClassifier_3", filename: filepath.Join(s.samplesDir, "C/main.c"), candidates: []string{}, classifier: DefaultClassifier, expected: "C"},
{name: "TestGetLanguagesByClassifier_4", filename: filepath.Join(samples, "C/blob.c"), candidates: []string{"python", "ruby", "c++"}, classifier: DefaultClassifier, expected: "C++"}, {name: "TestGetLanguagesByClassifier_4", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: []string{"python", "ruby", "c++"}, classifier: DefaultClassifier, expected: "C++"},
{name: "TestGetLanguagesByClassifier_5", filename: filepath.Join(samples, "C/blob.c"), candidates: []string{"ruby"}, classifier: DefaultClassifier, expected: "Ruby"}, {name: "TestGetLanguagesByClassifier_5", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: []string{"ruby"}, classifier: DefaultClassifier, expected: "Ruby"},
{name: "TestGetLanguagesByClassifier_6", filename: filepath.Join(samples, "Python/django-models-base.py"), candidates: []string{"python", "ruby", "c", "c++"}, classifier: DefaultClassifier, expected: "Python"}, {name: "TestGetLanguagesByClassifier_6", filename: filepath.Join(s.samplesDir, "Python/django-models-base.py"), candidates: []string{"python", "ruby", "c", "c++"}, classifier: DefaultClassifier, expected: "Python"},
{name: "TestGetLanguagesByClassifier_6", filename: os.DevNull, candidates: nil, classifier: DefaultClassifier, expected: OtherLanguage}, {name: "TestGetLanguagesByClassifier_6", filename: os.DevNull, candidates: nil, classifier: DefaultClassifier, expected: OtherLanguage},
} }
@ -373,14 +371,13 @@ func (s *EnryTestSuite) TestGetLanguageByAlias() {
func (s *EnryTestSuite) TestLinguistCorpus() { func (s *EnryTestSuite) TestLinguistCorpus() {
const filenamesDir = "filenames" const filenamesDir = "filenames"
var samplesDir = filepath.Join(s.repoLinguist, "samples")
var cornerCases = map[string]bool{ var cornerCases = map[string]bool{
"hello.ms": true, "hello.ms": true,
} }
var total, failed, ok, other int var total, failed, ok, other int
var expected string var expected string
filepath.Walk(samplesDir, func(path string, f os.FileInfo, err error) error { filepath.Walk(s.samplesDir, func(path string, f os.FileInfo, err error) error {
if f.IsDir() { if f.IsDir() {
if f.Name() != filenamesDir { if f.Name() != filenamesDir {
expected = f.Name() expected = f.Name()