From 0c2e47d1b88e54fbea6efad80422135cf9e07bdc Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Thu, 23 Jun 2022 08:57:30 +0200 Subject: linguist: Test language compatibility with go-enry To make sure we're not breaking things when we'll switch to go-enry for the language detection, compare the known languages of the linguist gem with the Go package. --- internal/gitaly/linguist/linguist_test.go | 49 +++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/internal/gitaly/linguist/linguist_test.go b/internal/gitaly/linguist/linguist_test.go index 2de3136a0..6de429e1a 100644 --- a/internal/gitaly/linguist/linguist_test.go +++ b/internal/gitaly/linguist/linguist_test.go @@ -7,6 +7,8 @@ import ( "path/filepath" "testing" + "github.com/go-enry/go-enry/v2" + enrydata "github.com/go-enry/go-enry/v2/data" "github.com/sirupsen/logrus" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/require" @@ -25,6 +27,53 @@ func TestMain(m *testing.M) { testhelper.Run(m) } +func TestNew_knownLanguages(t *testing.T) { + cfg := testcfg.Build(t, testcfg.WithRealLinguist()) + gitCmdFactory := gittest.NewCommandFactory(t, cfg) + + linguist, err := New(cfg, gitCmdFactory) + require.NoError(t, err) + + t.Run("by name", func(t *testing.T) { + linguistLanguages := make([]string, 0, len(linguist.colorMap)) + for language := range linguist.colorMap { + linguistLanguages = append(linguistLanguages, language) + } + + enryLanguages := make([]string, 0, len(enrydata.IDByLanguage)) + for language := range enrydata.IDByLanguage { + enryLanguages = append(enryLanguages, language) + } + + require.ElementsMatch(t, linguistLanguages, enryLanguages) + }) + + t.Run("with their color", func(t *testing.T) { + exclude := map[string]struct{}{} + + linguistLanguages := make(map[string]string, len(linguist.colorMap)) + for language, color := range linguist.colorMap { + if color.Color == "" { + exclude[language] = struct{}{} + continue + } + linguistLanguages[language] = color.Color + } + + enryLanguages := make(map[string]string, len(enrydata.IDByLanguage)) + for language := range enrydata.IDByLanguage { + if _, excluded := exclude[language]; excluded { + continue + } + + color := enry.GetColor(language) + enryLanguages[language] = color + } + + require.Equal(t, linguistLanguages, enryLanguages) + }) +} + func TestInstance_Stats(t *testing.T) { testhelper.NewFeatureSets(featureflag.GoLanguageStats). Run(t, testInstanceStats) -- cgit v1.2.3