Skip to content

feat: check sources directory exists and is a directory#43

Merged
feloy merged 2 commits intokortex-hub:mainfrom
feloy:check-src-dir
Mar 10, 2026
Merged

feat: check sources directory exists and is a directory#43
feloy merged 2 commits intokortex-hub:mainfrom
feloy:check-src-dir

Conversation

@feloy
Copy link
Copy Markdown
Contributor

@feloy feloy commented Mar 10, 2026

Fixes #40

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 10, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6a40d5cd-ecb3-4341-8976-480e74fab68c

📥 Commits

Reviewing files that changed from the base of the PR and between b1ef9e1 and c378707.

📒 Files selected for processing (1)
  • pkg/cmd/init_test.go

📝 Walkthrough

Walkthrough

The init command's preRun now resolves the absolute sources directory and verifies it exists and is a directory, returning distinct errors for missing path, stat failures, or non-directory paths. Tests were expanded to cover these failure cases and extended E2E scenarios.

Changes

Cohort / File(s) Summary
Init command validation
pkg/cmd/init.go
Added os import and preRun checks that stat the absolute sources directory: return explicit error if the path does not exist, wrap stat errors, and error if the path exists but is not a directory.
Init command tests
pkg/cmd/init_test.go
Refactored tests to manage working-directory for relative-path cases, created source dirs for E2E flows, added negative tests for non-existent sources and sources-as-file, adjusted parallelization and setup/teardown, and expanded assertions for workspace registration results.

Sequence Diagram(s)

(omitted — change is a localized validation check and test updates, not a multi-component flow that warrants a sequence diagram)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • feat: init command #23: Modifies pkg/cmd/init.go and tests with related init preRun validation and test case changes.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding validation that the sources directory exists and is a directory during the init command.
Description check ✅ Passed The description references the linked issue #40, which relates to the changeset's objective of validating source directory existence.
Linked Issues check ✅ Passed The code changes fully implement the requirement from issue #40: validation is added to fail when the sources directory does not exist or is not a directory.
Out of Scope Changes check ✅ Passed All changes are focused on implementing directory existence validation as specified in issue #40; no out-of-scope modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
pkg/cmd/init_test.go (1)

206-206: Use a non-default relative path in this subtest.

With relativePath := ".", this case now overlaps heavily with "default arguments" and no longer really exercises resolution of an explicit relative source path. Using a real relative path would keep coverage on the branch this test is named after.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/cmd/init_test.go` at line 206, The test currently sets relativePath :=
"." which duplicates the default-arguments case; change relativePath to a real
non-default relative path (e.g. "./some/relative/path" or a temp subdirectory
created in the test) so the subtest actually exercises explicit relative source
resolution; update any setup to create that directory (or file) and adjust
expected outputs/assertions accordingly for the code paths exercised by Funcs
that consume relativePath (locate the variable named relativePath in this
subtest and ensure the created path is cleaned up after the test).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@pkg/cmd/init_test.go`:
- Line 206: The test currently sets relativePath := "." which duplicates the
default-arguments case; change relativePath to a real non-default relative path
(e.g. "./some/relative/path" or a temp subdirectory created in the test) so the
subtest actually exercises explicit relative source resolution; update any setup
to create that directory (or file) and adjust expected outputs/assertions
accordingly for the code paths exercised by Funcs that consume relativePath
(locate the variable named relativePath in this subtest and ensure the created
path is cleaned up after the test).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4e7342bd-5633-4262-9859-ace667037780

📥 Commits

Reviewing files that changed from the base of the PR and between 1ecb0a3 and 7e2fd49.

📒 Files selected for processing (2)
  • pkg/cmd/init.go
  • pkg/cmd/init_test.go

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/cmd/init_test.go (1)

202-242: ⚠️ Potential issue | 🟡 Minor

Use t.TempDir() for the relative path test to avoid polluting the working directory.

The test creates ./relative/path in the current working directory, which leaves artifacts behind and could interfere with parallel tests. Instead, create the relative path structure inside a temp directory and change the working directory for the test.

As per coding guidelines: "Use t.TempDir() for temporary directories in tests for automatic cleanup and cross-platform compatibility"

🛠️ Proposed fix using t.TempDir() with os.Chdir
 	t.Run("relative sources directory", func(t *testing.T) {
-		t.Parallel()
+		// Cannot use t.Parallel() because we're changing the working directory

 		tempDir := t.TempDir()
-		relativePath := filepath.Join(".", "relative", "path")
 
-		// Create the relative directory so it exists
-		if err := os.MkdirAll(relativePath, 0755); err != nil {
-			t.Fatalf("Failed to create relative directory: %v", err)
+		// Save current directory and restore after test
+		originalDir, err := os.Getwd()
+		if err != nil {
+			t.Fatalf("Failed to get current directory: %v", err)
+		}
+		defer os.Chdir(originalDir)
+
+		// Change to temp directory
+		if err := os.Chdir(tempDir); err != nil {
+			t.Fatalf("Failed to change directory: %v", err)
 		}
 
+		relativePath := filepath.Join(".", "relative", "path")
+		if err := os.MkdirAll(relativePath, 0755); err != nil {
+			t.Fatalf("Failed to create relative directory: %v", err)
+		}
+
 		c := &initCmd{}

Note: This approach requires removing t.Parallel() since os.Chdir affects the entire process. Alternatively, consider testing with an absolute path that simulates the relative path scenario.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/cmd/init_test.go` around lines 202 - 242, The test creates
./relative/path in the real working directory; switch to using t.TempDir() and
change the process working directory for the test so artifacts are cleaned up:
inside the "relative sources directory" test use tempRoot := t.TempDir(), create
the relative structure under tempRoot (e.g. filepath.Join(tempRoot, "relative",
"path")), then save cwd := os.Getwd(); defer os.Chdir(cwd); os.Chdir(tempRoot)
so the relativePath := filepath.Join(".", "relative", "path") refers to the temp
area; remove t.Parallel() because os.Chdir affects the whole process; keep
assertions against c.sourcesDir, c.absSourcesDir (use
filepath.Abs(relativePath)), and c.workspaceConfigDir
(filepath.Join(relativePath, ".kortex")) unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@pkg/cmd/init_test.go`:
- Around line 202-242: The test creates ./relative/path in the real working
directory; switch to using t.TempDir() and change the process working directory
for the test so artifacts are cleaned up: inside the "relative sources
directory" test use tempRoot := t.TempDir(), create the relative structure under
tempRoot (e.g. filepath.Join(tempRoot, "relative", "path")), then save cwd :=
os.Getwd(); defer os.Chdir(cwd); os.Chdir(tempRoot) so the relativePath :=
filepath.Join(".", "relative", "path") refers to the temp area; remove
t.Parallel() because os.Chdir affects the whole process; keep assertions against
c.sourcesDir, c.absSourcesDir (use filepath.Abs(relativePath)), and
c.workspaceConfigDir (filepath.Join(relativePath, ".kortex")) unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b4e2f37e-cf8c-4112-8edd-e1c51870a4ec

📥 Commits

Reviewing files that changed from the base of the PR and between 7e2fd49 and 494c059.

📒 Files selected for processing (2)
  • pkg/cmd/init.go
  • pkg/cmd/init_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/cmd/init.go

@feloy feloy requested review from benoitf and jeffmaury March 10, 2026 09:33
Signed-off-by: Philippe Martin <phmartin@redhat.com>

Co-Authored-By: Claude Code (Claude Sonnet 4.5) <noreply@anthropic.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pkg/cmd/init_test.go`:
- Around line 202-225: The subtest t.Run("relative sources directory") mutates
the process working directory (os.Chdir) which is still unsafe because its
parent test is parallelized; either extract this case into its own top-level
Test (e.g., TestInitCmd_RelativeSourcesDirectory) that does not call
t.Parallel(), or stop mutating cwd by running the code under test with an
explicit working directory: create the same relative path inside workDir and
resolve paths via filepath.Abs or run the command with its Dir set (or pass the
absolute source path computed from workDir+relativePath) instead of calling
os.Chdir in the subtest; update the test named "relative sources directory" to
use one of these approaches and remove os.Chdir to avoid races with
TestInitCmd_E2E and other parallel tests.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a22bd2ea-23c8-4b20-94e1-ce088abf7352

📥 Commits

Reviewing files that changed from the base of the PR and between 7411861 and b1ef9e1.

📒 Files selected for processing (2)
  • pkg/cmd/init.go
  • pkg/cmd/init_test.go

Signed-off-by: Philippe Martin <phmartin@redhat.com>
@feloy feloy merged commit 1971004 into kortex-hub:main Mar 10, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

check sources directory exists

2 participants