package log_extractor_test

import (
	"go/build"
	"testing"

	"github.com/pingcap-inc/alioth/extractor/go/compiler"
	logextractor "github.com/pingcap-inc/alioth/extractor/go/log"
	"github.com/pingcap-inc/alioth/pkg/worker"
	. "github.com/pingcap/check"
)

func TestClient(t *testing.T) {
	TestingT(t)
}

var _ = Suite(&testLogExtractSuite{})

type testLogExtractSuite struct {
}

func (t *testLogExtractSuite) TestPackageCompile(c *C) {
	testPackageImportPath := "../../../test/ast_test"

	builder := &logextractor.Builder{}

	poolWorker := worker.NewPool(4, "log_extract_test")
	repo, err := builder.Build(build.Default, testPackageImportPath, []string{"example_ignore"}, poolWorker)
	c.Assert(err, IsNil)
	c.Assert(repo.GetRepoPath(), Equals, "github.com/pingcap-inc/alioth")

	compilations := map[string][]string{
		"github.com/pingcap-inc/alioth/test/ast_test/example":          {"print.go"},
		"github.com/pingcap-inc/alioth/test/ast_test/multiple_main":    {"main1.go", "main2.go"},
		"github.com/pingcap-inc/alioth/test/ast_test/multiple_package": {"pkg1.go", "pkg2.go"},
		"github.com/pingcap-inc/alioth/test/ast_test":                  {"main.go"},
	}

	repo.ForEach(func(pc *compiler.PackageCompilation) error {
		c.Assert(pc.DepOnly, IsFalse)

		goFiles, ok := compilations[pc.ImportPath]
		c.Assert(ok, IsTrue)
		files := make([]string, 0, len(pc.SourceFileSet))
		for file := range pc.SourceFileSet {
			files = append(files, file)
		}
		c.Assert(files, DeepEquals, goFiles)
		delete(compilations, pc.ImportPath)
		return nil
	})
	c.Assert(compilations, HasLen, 2)
	c.Assert(compilations, HasKey, "github.com/pingcap-inc/alioth/test/ast_test/multiple_main")
	c.Assert(compilations, HasKey, "github.com/pingcap-inc/alioth/test/ast_test/multiple_package")
}
