Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
tar -xzf "$RUNNER_TEMP/$ARCHIVE"
echo "Sign in" > en/guide
echo "로그인" > ko/guide
export TGREP_ENTER_ACTION=print FZF_DEFAULT_OPTS=--filter=guide
export TGREP_TARGET_LANGS=ko TGREP_ENTER_ACTION=print FZF_DEFAULT_OPTS=--filter=guide
test "$(./tgrep 'sign in')" = './en/guide:1:Sign in'
- uses: actions/upload-artifact@v7
with:
Expand Down
2 changes: 2 additions & 0 deletions README.jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ TGREP_TARGET_LANGS=ko,ja
TGREP_ENTER_ACTION=edit
```

検索を始める前に、原文と設定したすべての翻訳言語のディレクトリを確認します。見つからないディレクトリがあると、そのパスを表示して終了します。英語の原文と韓国語訳だけがある場合は、設定ファイルに `TGREP_TARGET_LANGS=ko` を指定してください。

| 設定項目 | 既定値 | 説明 |
| --- | --- | --- |
| `TGREP_DOCS_DIR` | `.` | 言語別ディレクトリを格納する親ディレクトリ。相対パスは現在の作業ディレクトリを基準に解決されます。 |
Expand Down
2 changes: 2 additions & 0 deletions README.kr.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ TGREP_TARGET_LANGS=ko,ja
TGREP_ENTER_ACTION=edit
```

검색 전에 원문 디렉터리와 설정에 지정한 모든 번역 디렉터리를 확인합니다. 디렉터리가 없으면 해당 경로를 표시하고 종료합니다. 영어 원문과 한국어 번역만 있다면 설정 파일에 `TGREP_TARGET_LANGS=ko`를 지정하세요.

| 설정 | 기본값 | 설명 |
| --- | --- | --- |
| `TGREP_DOCS_DIR` | `.` | 언어별 디렉터리를 담고 있는 상위 디렉터리입니다. 상대 경로는 현재 작업 디렉터리를 기준으로 합니다. |
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ TGREP_TARGET_LANGS=ko,ja
TGREP_ENTER_ACTION=edit
```

Before searching, tgrep checks the source directory and every configured translation directory. If a directory is missing, tgrep lists the affected paths and exits. If you have only English and Korean documents, set `TGREP_TARGET_LANGS=ko` in your config.

| Setting | Default | Meaning |
| --- | --- | --- |
| `TGREP_DOCS_DIR` | `.` | Directory containing the language directories. Relative paths start at the current working directory. |
Expand Down
27 changes: 27 additions & 0 deletions cmd/tgrep/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func TestCLI(t *testing.T) {
rel := "guide/a:b's file"
writeFile(t, filepath.Join(root, "en", rel), "before\nSIGN IN: now\nafter\n", 0644)
writeFile(t, filepath.Join(root, "ja", rel), "前\nログイン\n次\n", 0644)
if err := os.Mkdir(filepath.Join(root, "ko"), 0755); err != nil {
t.Fatal(err)
}
cfg := filepath.Join(root, ".config/tgrep/config")
writeFile(t, cfg, "TGREP_TARGET_LANGS=ko,ja\nTGREP_ENTER_ACTION=edit\nTGREP_AFTER_LINES_JA=0\n", 0644)
env := []string{"FZF_DEFAULT_OPTS=--filter=guide", "TGREP_ENTER_ACTION=print"}
Expand Down Expand Up @@ -86,6 +89,27 @@ func TestCLI(t *testing.T) {
}
}

func TestMissingTargetDirectory(t *testing.T) {
root := t.TempDir()
writeFile(t, filepath.Join(root, "en/doc"), "Sign in\n", 0644)
writeFile(t, filepath.Join(root, "ko/doc"), "로그인\n", 0644)
env := []string{"TGREP_ENTER_ACTION=print", "FZF_DEFAULT_OPTS=--filter=doc"}
out, log, code := invoke(t, root, env, []string{"sign in"})
if code != 1 || out != "" {
t.Fatalf("missing ja: exit %d, stdout %q, stderr %q", code, out, log)
}
for _, want := range []string{"./ja", "~/.config/tgrep/config", "TGREP_DOCS_DIR", "TGREP_TARGET_LANGS=ko"} {
if !strings.Contains(log, want) {
t.Errorf("missing %q in stderr: %q", want, log)
}
}
writeFile(t, filepath.Join(root, ".config/tgrep/config"), "TGREP_TARGET_LANGS=ko\n", 0644)
out, log, code = invoke(t, root, env, []string{"sign in"})
if code != 0 || out != "./en/doc:1:Sign in\n" {
t.Fatalf("configured target: exit %d, stdout %q, stderr %q", code, out, log)
}
}

func TestZshConfig(t *testing.T) {
zsh, err := exec.LookPath("zsh")
if err != nil {
Expand All @@ -94,6 +118,9 @@ func TestZshConfig(t *testing.T) {
root := t.TempDir()
writeFile(t, filepath.Join(root, "src/en/doc"), "HIT\n", 0644)
writeFile(t, filepath.Join(root, "src/korean/docs/doc"), "번역\n", 0644)
if err := os.Mkdir(filepath.Join(root, "src/ja"), 0755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(root, "bin"), 0755); err != nil {
t.Fatal(err)
}
Expand Down
35 changes: 24 additions & 11 deletions internal/documents/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,34 @@ func (t Tree) files(lang string) ([]string, error) {
return files, err
}

func (t Tree) PairedFiles() ([]string, error) {
func (t Tree) validateDirectories() error {
c := t.Config
originDir := t.Path(c.Origin, "")
if info, err := os.Stat(originDir); err != nil || !info.IsDir() {
return nil, fmt.Errorf("Origin directory not found: %s", originDir)
var problems []string
for _, lang := range append([]string{c.Origin}, c.Targets...) {
dir := t.Path(lang, "")
info, err := os.Stat(dir)
if err != nil {
problems = append(problems, fmt.Sprintf(" %s: %v", lang, err))
} else if !info.IsDir() {
problems = append(problems, fmt.Sprintf(" %s: %s is not a directory", lang, dir))
}
}
if len(problems) > 0 {
return fmt.Errorf("invalid language directories:\n%s\n"+
"In ~/.config/tgrep/config, set TGREP_DOCS_DIR to the directory containing your language directories.\n"+
"Set TGREP_ORIGIN_LANG and TGREP_TARGET_LANGS to the languages you have (for example, TGREP_TARGET_LANGS=ko).\n"+
"For a language stored elsewhere, set its root (for example, DOCS_KO_ROOT=/path/to/korean).", strings.Join(problems, "\n"))
}
return nil
}

func (t Tree) PairedFiles() ([]string, error) {
if err := t.validateDirectories(); err != nil {
return nil, err
}
c := t.Config
translated := make(map[string]bool)
for _, lang := range c.Targets {
info, err := os.Stat(t.Path(lang, ""))
if os.IsNotExist(err) || (err == nil && !info.IsDir()) {
continue
}
if err != nil {
return nil, err
}
files, err := t.files(lang)
if err != nil {
return nil, err
Expand Down
25 changes: 24 additions & 1 deletion internal/documents/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"

"github.com/antfrog/tgrep/internal/config"
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestPairedFiles(t *testing.T) {
if err := os.Remove(filepath.Join(docs, "ko/gone")); err != nil {
t.Fatal(err)
}
tree := Open(config.Config{Root: docs, Origin: "en", Targets: []string{"ko", "missing", "ja"}})
tree := Open(config.Config{Root: docs, Origin: "en", Targets: []string{"ko", "ja"}})
got, err := tree.PairedFiles()
if err != nil {
t.Fatal(err)
Expand All @@ -79,6 +80,28 @@ func TestPairedFiles(t *testing.T) {
}
}

func TestPairedFilesReportsInvalidDirectories(t *testing.T) {
root := t.TempDir()
file := filepath.Join(root, "regular file")
if err := os.WriteFile(file, nil, 0644); err != nil {
t.Fatal(err)
}
c := config.Config{Root: root, Origin: "fr", Targets: []string{"ko", "ja", "de"},
LanguageRoots: map[string]string{"fr": "source", "ko": file, "ja": t.TempDir()}}
paths, err := Open(c).PairedFiles()
if err == nil || len(paths) != 0 {
t.Fatalf("PairedFiles() = %q, %v", paths, err)
}
for _, want := range []string{filepath.Join(root, "source"), file + " is not a directory", filepath.Join(root, "de")} {
if !strings.Contains(err.Error(), want) {
t.Errorf("missing %q in error: %v", want, err)
}
}
if strings.Contains(err.Error(), c.LanguageRoots["ja"]) {
t.Errorf("valid directory reported: %v", err)
}
}

func TestPairedFilesAcrossRepositories(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git unavailable")
Expand Down
Loading