-
Notifications
You must be signed in to change notification settings - Fork 7
[AGENT-628] Unit tests #441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Local development files (.env.development, .env.local, etc.) should never be synced to the production database. This was causing the CLI to prompt users to sync development-only environment variables to their cloud project. The fix adds a conditional check to only call HandleMissingProjectEnvs when not in local development mode (isLocalDev = false). Fixes issue where users were getting prompted to sync env vars from .env.development files to production. Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-94a6a673-ef07-4dbd-95a8-8437d17bdd21
…ests - Extract DetermineEnvFilename() for env file selection logic - Extract ParseAndProcessEnvFile() for file parsing and template processing - Extract ShouldSyncToProduction() for production sync decision logic - Add comprehensive unit tests covering: - Environment file selection (production vs development) - File creation when .env.development doesn't exist - Production sync conditional logic based on isLocalDev flag - Fix bug where created .env.development file path wasn't returned correctly This refactor makes the code more maintainable and ensures the core logic for preventing local development env vars from syncing to production is well-tested. Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-94a6a673-ef07-4dbd-95a8-8437d17bdd21
|
Caution Review failedThe pull request is closed. WalkthroughRefactors env-file handling by adding DetermineEnvFilename, ParseAndProcessEnvFile, and ShouldSyncToProduction; ProcessEnvFiles now prefers/creates Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User
participant CLI
participant EnvUtil as EnvUtil.ProcessEnvFiles
participant FS as FileSystem
participant Parser as ParseAndProcessEnvFile
participant Deployer as HandleMissingProjectEnvs
User->>CLI: run command
CLI->>EnvUtil: ProcessEnvFiles(dir, isLocalDev, ...)
EnvUtil->>FS: DetermineEnvFilename(dir, isLocalDev)
alt local-dev & .env.development missing
FS-->>EnvUtil: not found
EnvUtil->>FS: create .env.development
FS-->>EnvUtil: path -> .env.development
else file exists
FS-->>EnvUtil: path -> chosen env file
end
EnvUtil->>Parser: ParseAndProcessEnvFile(logger, dir, envfilename, force)
Parser-->>EnvUtil: EnvFile (processed)
alt ShouldSyncToProduction(isLocalDev=false)
EnvUtil->>Deployer: HandleMissingProjectEnvs(...)
Deployer-->>EnvUtil: sync result
else isLocalDev=true
Note over EnvUtil: skip production sync
end
EnvUtil-->>CLI: processed EnvFile / result
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Assessment against linked issues
Possibly related PRs
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/envutil/envutil.go (1)
41-49: Create .env.development with 0600 perms and check write errors.os.Create honors umask (often 022), which can leave the file world-readable. This file will likely hold secrets; default to 0600 and don’t ignore write errors.
- // create it but don't load it -- this is required because uv expects a .env.development file to exist from the template - // but since its gitignore it won't get checked in and might not exist when you clone a project - of, err := os.Create(f) - if err != nil { - return "", err - } - defer of.Close() - of.WriteString("# This file is used to store development environment variables\n") + // Create (required by templates); safe to be empty. Use restrictive permissions. + if err := os.WriteFile(f, []byte("# This file is used to store development environment variables\n"), 0600); err != nil { + return "", err + } envfilename = f
🧹 Nitpick comments (4)
internal/envutil/envutil_test.go (2)
82-108: Make this test parallel-safe.Tests are table-driven and independent; run in parallel for speed.
func TestShouldSyncToProduction(t *testing.T) { + t.Parallel()
110-189: Parallelize subtests and cover the “no files exist” case.
- Parallelization: safe, improves throughput.
- Add case where neither .env nor .env.development exists; function should still create .env.development.
func TestDetermineEnvFilename(t *testing.T) { + t.Parallel() @@ tests := []struct { @@ }{ @@ { name: "local dev mode creates .env.development when it doesn't exist", isLocalDev: true, existingFiles: []string{".env"}, expectedFile: ".env.development", shouldCreateDev: true, }, + { + name: "local dev mode creates .env.development when no files exist", + isLocalDev: true, + existingFiles: []string{}, + expectedFile: ".env.development", + shouldCreateDev: true, + }, } @@ for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + t.Parallel()internal/envutil/envutil.go (2)
41-43: Align comment with behavior (file is actually loaded).The comment says “don’t load it,” but ProcessEnvFiles will parse it when it exists. Either update the comment or add logic to skip parsing newly created placeholder files. Given current behavior, updating the comment is simplest.
- // create it but don't load it -- this is required because uv expects a .env.development file to exist from the template - // but since its gitignore it won't get checked in and might not exist when you clone a project + // Create it (required by templates). It's safe if empty; downstream parsing will just yield no variables.Also applies to: 90-91, 96-99
83-87: Generalize the error context message.This path can fail for reasons beyond create; make the message mode-agnostic.
- errsystem.New(errsystem.ErrInvalidConfiguration, err, errsystem.WithContextMessage("Failed to create .env.development file")).ShowErrorAndExit() + errsystem.New(errsystem.ErrInvalidConfiguration, err, errsystem.WithContextMessage("Failed to prepare env file")).ShowErrorAndExit()
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
internal/envutil/envutil.go(2 hunks)internal/envutil/envutil_test.go(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
internal/envutil/envutil_test.go (1)
internal/envutil/envutil.go (2)
ShouldSyncToProduction(75-79)DetermineEnvFilename(34-53)
internal/envutil/envutil.go (5)
internal/deployer/deployer.go (1)
EnvFile(15-18)internal/project/project.go (1)
ProjectData(44-53)internal/errsystem/errorcodes.go (2)
ErrInvalidConfiguration(25-28)ErrParseEnvironmentFile(41-44)internal/errsystem/errsystem.go (1)
WithContextMessage(100-104)internal/util/io.go (1)
Exists(14-19)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Analyze (go)
🔇 Additional comments (3)
internal/envutil/envutil_test.go (1)
4-7: LGTM on added imports.os and path/filepath are appropriate for the filesystem-based tests.
internal/envutil/envutil.go (2)
55-73: Helper extraction looks good.Encapsulates template merge + parse cleanly; return type matches deployer.EnvFile usage.
74-80: Production sync gating is correct.Clear, boolean-based guard avoids accidental local→prod sync.
…8-TESTS # Conflicts: # internal/envutil/envutil.go
- Added: [AGENT-684] Check if zsh is installed before adding autocomplete in the CLI (#450) - Added: [AGENT-628] Unit tests (#441) - Added: feat: automatically add AGENTUITY_SDK_KEY and AGENTUITY_PROJECT_KEY to .env file when running dev command (#442) - Changed: Dont sort releases by commit msg (#447) - Changed: [AGENT-628] prevent local development env files from syncing to production (#440) - Fixed: Fix npm workspaces (#451) - Fixed: Fix 'Press any key to continue' to accept any key, not just Enter (#445) Co-Authored-By: unknown <>
- Added: [AGENT-684] Check if zsh is installed before adding autocomplete in the CLI (#450) - Added: [AGENT-628] Unit tests (#441) - Added: feat: automatically add AGENTUITY_SDK_KEY and AGENTUITY_PROJECT_KEY to .env file when running dev command (#442) - Changed: Dont sort releases by commit msg (#447) - Changed: [AGENT-628] prevent local development env files from syncing to production (#440) - Fixed: Fix npm workspaces (#451) - Fixed: Fix 'Press any key to continue' to accept any key, not just Enter (#445) Co-Authored-By: unknown <> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Summary by CodeRabbit
New Features
Refactor
Tests