diff options
Diffstat (limited to 'internal/linguist')
-rw-r--r-- | internal/linguist/linguist.go | 31 | ||||
-rw-r--r-- | internal/linguist/linguist_test.go | 32 | ||||
-rw-r--r-- | internal/linguist/testdata/fake-languages.json | 3 |
3 files changed, 57 insertions, 9 deletions
diff --git a/internal/linguist/linguist.go b/internal/linguist/linguist.go index f2158a566..9c7feeb2f 100644 --- a/internal/linguist/linguist.go +++ b/internal/linguist/linguist.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/json" "fmt" + "io" "io/ioutil" "os" "os/exec" @@ -53,14 +54,31 @@ func Color(language string) string { // LoadColors loads the name->color map from the Linguist gem. func LoadColors() error { - linguistPathSymlink, err := ioutil.TempFile("", "gitaly-linguist-path") + jsonReader, err := openLanguagesJSON() if err != nil { return err } + defer jsonReader.Close() + + return json.NewDecoder(jsonReader).Decode(&colorMap) +} + +func openLanguagesJSON() (io.ReadCloser, error) { + if jsonPath := config.Config.Ruby.LinguistLanguagesPath; jsonPath != "" { + // This is a fallback for environments where dynamic discovery of the + // linguist path via Bundler is not working for some reason, for example + // https://gitlab.com/gitlab-org/gitaly/issues/1119. + return os.Open(jsonPath) + } + + linguistPathSymlink, err := ioutil.TempFile("", "gitaly-linguist-path") + if err != nil { + return nil, err + } defer os.Remove(linguistPathSymlink.Name()) if err := linguistPathSymlink.Close(); err != nil { - return err + return nil, err } // We use a symlink because we cannot trust Bundler to not print garbage @@ -73,13 +91,8 @@ func LoadColors() error { if exitError, ok := err.(*exec.ExitError); ok { err = fmt.Errorf("%v; stderr: %q", exitError, exitError.Stderr) } - return err - } - - languageJSON, err := ioutil.ReadFile(path.Join(linguistPathSymlink.Name(), "lib/linguist/languages.json")) - if err != nil { - return err + return nil, err } - return json.Unmarshal(languageJSON, &colorMap) + return os.Open(path.Join(linguistPathSymlink.Name(), "lib/linguist/languages.json")) } diff --git a/internal/linguist/linguist_test.go b/internal/linguist/linguist_test.go new file mode 100644 index 000000000..aac91d866 --- /dev/null +++ b/internal/linguist/linguist_test.go @@ -0,0 +1,32 @@ +package linguist + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/internal/config" + "gitlab.com/gitlab-org/gitaly/internal/testhelper" +) + +func TestLoadLanguages(t *testing.T) { + testhelper.ConfigureRuby() + + colorMap = make(map[string]Language) + require.NoError(t, LoadColors(), "load colors") + + require.Equal(t, "#701516", Color("Ruby"), "color value for 'Ruby'") +} + +func TestLoadLanguagesCustomPath(t *testing.T) { + jsonPath, err := filepath.Abs("testdata/fake-languages.json") + require.NoError(t, err) + + testhelper.ConfigureRuby() + config.Config.Ruby.LinguistLanguagesPath = jsonPath + + colorMap = make(map[string]Language) + require.NoError(t, LoadColors(), "load colors") + + require.Equal(t, "foo color", Color("FooBar")) +} diff --git a/internal/linguist/testdata/fake-languages.json b/internal/linguist/testdata/fake-languages.json new file mode 100644 index 000000000..83f7687f8 --- /dev/null +++ b/internal/linguist/testdata/fake-languages.json @@ -0,0 +1,3 @@ +{ +"FooBar": { "color": "foo color" } +} |