Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Dec 27, 2024
2 parents 71b31ac + 8641937 commit 1a36513
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 9 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
name: Test Install Script
name: Test

on:
pull_request:
push:
branches: [main]

jobs:
test:
run-tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.23"
- run: make test

install-cli:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: dash -x install.sh
- run: codecrafters --version
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ brews:
commit_author:
name: Paul Kuruvilla
email: [email protected]
homepage: https://codecrafters.io.io
homepage: https://codecrafters.io
description: CodeCrafters CLI
license: MIT
# TODO: Add completions
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1
github.com/otiai10/copy v1.7.0
github.com/rs/zerolog v1.28.0
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/stretchr/testify v1.8.1
)

Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ github.com/rohitpaulk/asyncwriter v0.0.2/go.mod h1:y3Ja8mupuzm26lOnNtZ2TpELfHsGV
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -eu

# allow overriding the version
VERSION=${CODECRAFTERS_CLI_VERSION:-v34}
VERSION=${CODECRAFTERS_CLI_VERSION:-v35}

PLATFORM=$(uname -s)
ARCH=$(uname -m)
Expand Down
10 changes: 7 additions & 3 deletions internal/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
Expand Down Expand Up @@ -122,12 +121,17 @@ func TestCommand(ctx context.Context, shouldTestPrevious bool) (err error) {
}

func copyRepositoryDirToTempDir(repoDir string) (string, error) {
tmpDir, err := ioutil.TempDir("", "codecrafters")
tmpDir, err := os.MkdirTemp("", "codecrafters")

if err != nil {
return "", fmt.Errorf("create temp dir: %w", err)
}

err = cp.Copy(repoDir, tmpDir)
gitIgnore := utils.NewGitIgnore(repoDir)

err = cp.Copy(repoDir, tmpDir, cp.Options{
Skip: gitIgnore.SkipFile,
})
if err != nil {
return "", fmt.Errorf("copy files: %w", err)
}
Expand Down
64 changes: 64 additions & 0 deletions internal/utils/git_ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Package utils
package utils

import (
"os"
"os/exec"
"path/filepath"
"strings"

ignore "github.com/sabhiram/go-gitignore"
)

type GitIgnore struct {
baseDir string
localGitIgnore *ignore.GitIgnore
globalGitIgnore *ignore.GitIgnore
gitInfoExclude *ignore.GitIgnore
}

func NewGitIgnore(baseDir string) GitIgnore {
return GitIgnore{
baseDir: baseDir,
localGitIgnore: compileIgnorer(filepath.Join(baseDir, ".gitignore")),
globalGitIgnore: compileIgnorer(getGlobalGitIgnorePath()),
gitInfoExclude: compileIgnorer(filepath.Join(baseDir, ".git", "info", "exclude")),
}
}

func (i GitIgnore) SkipFile(path string) (bool, error) {
for _, ignorer := range []*ignore.GitIgnore{i.localGitIgnore, i.globalGitIgnore, i.gitInfoExclude} {
if ignorer != nil && ignorer.MatchesPath(path) {
return true, nil
}
}

return false, nil
}

func compileIgnorer(path string) *ignore.GitIgnore {
ignorer, err := ignore.CompileIgnoreFile(path)
if err != nil {
return nil
}

return ignorer
}

func getGlobalGitIgnorePath() string {
output, err := exec.Command("git", "config", "--get", "core.excludesfile").Output()
if err != nil {
return ""
}

path := strings.TrimSpace(string(output))
if strings.HasPrefix(path, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
return ""
}
path = filepath.Join(homeDir, path[2:])
}

return path
}
119 changes: 119 additions & 0 deletions internal/utils/git_ignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package utils

import (
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGitIgnore(t *testing.T) {
t.Run("without gitignore files", func(t *testing.T) {
gitIgnore := NewGitIgnore(t.TempDir())
assertFileNotSkipped(t, &gitIgnore, "some/random/file.txt")
})

t.Run("with local gitignore", func(t *testing.T) {
tmpRepoDir := t.TempDir()
writeFile(t, filepath.Join(tmpRepoDir, ".gitignore"), "ignore/this/file.txt")

gitIgnore := NewGitIgnore(tmpRepoDir)
assertFileSkipped(t, &gitIgnore, "ignore/this/file.txt")
assertFileNotSkipped(t, &gitIgnore, "some/other/file.txt")
})

t.Run("with global gitignore", func(t *testing.T) {
backup := setupGlobalGitIgnore(t, "ignore/this/file.txt")
defer func() {
if backup.originalPath == "" {
unsetGlobalGitIgnoreConfig(t)
}
backup.Restore(t)
}()

gitIgnore := NewGitIgnore(t.TempDir())
assertFileSkipped(t, &gitIgnore, "ignore/this/file.txt")
assertFileNotSkipped(t, &gitIgnore, "some/other/file.txt")
})

t.Run("with git info exclude", func(t *testing.T) {
tmpRepoDir := createEmptyRepository(t)
backup := setupGitInfoExclude(t, tmpRepoDir, "ignore/this/file.txt")
defer backup.Restore(t)

gitIgnore := NewGitIgnore(tmpRepoDir)
assertFileSkipped(t, &gitIgnore, "ignore/this/file.txt")
assertFileNotSkipped(t, &gitIgnore, "some/other/file.txt")
})
}

func assertFileSkipped(t *testing.T, gitIgnore *GitIgnore, path string) {
skip, err := gitIgnore.SkipFile(path)
assert.NoError(t, err)
assert.True(t, skip)
}

func assertFileNotSkipped(t *testing.T, gitIgnore *GitIgnore, path string) {
skip, err := gitIgnore.SkipFile(path)
assert.NoError(t, err)
assert.False(t, skip)
}

type FileBackup struct {
originalPath string
backupPath string
}

func (b *FileBackup) Restore(t *testing.T) {
if b.originalPath != "" {
moveFile(t, b.backupPath, b.originalPath)
}
}

func setupGlobalGitIgnore(t *testing.T, content string) *FileBackup {
globalGitIgnorePath := getGlobalGitIgnorePath()
backupPath := filepath.Join(t.TempDir(), ".gitignore_global")

if globalGitIgnorePath == "" {
writeFile(t, backupPath, content)
setGlobalGitIgnoreConfig(t, backupPath)
return &FileBackup{originalPath: "", backupPath: backupPath}
}

moveFile(t, globalGitIgnorePath, backupPath)
writeFile(t, globalGitIgnorePath, content)
return &FileBackup{originalPath: globalGitIgnorePath, backupPath: backupPath}
}

func setupGitInfoExclude(t *testing.T, baseDir string, content string) *FileBackup {
gitInfoExcludePath := filepath.Join(baseDir, ".git", "info", "exclude")
_, err := os.Stat(gitInfoExcludePath)
assert.NoError(t, err)

backupPath := filepath.Join(t.TempDir(), ".git_info_exclude_backup")
moveFile(t, gitInfoExcludePath, backupPath)
writeFile(t, gitInfoExcludePath, content)
return &FileBackup{originalPath: gitInfoExcludePath, backupPath: backupPath}
}

func setGlobalGitIgnoreConfig(t *testing.T, path string) {
_, err := exec.Command("git", "config", "--global", "core.excludesfile", path).Output()
assert.NoError(t, err)
}

func unsetGlobalGitIgnoreConfig(t *testing.T) {
_, err := exec.Command("git", "config", "--global", "--unset", "core.excludesfile").Output()
assert.NoError(t, err)
}

func moveFile(t *testing.T, srcPath string, dstPath string) {
err := os.Rename(srcPath, dstPath)
assert.NoError(t, err)
}

func writeFile(t *testing.T, path string, content string) {
err := os.WriteFile(path, []byte(content), 0644)
assert.NoError(t, err)
}

0 comments on commit 1a36513

Please sign in to comment.