Skip to content

Conversation

@ulyssessouza
Copy link
Owner

No description provided.

Increase test coverage for the dao package from 0% to 100% by adding
23 comprehensive tests covering all DAO functionality including:
- Constructor variants (NewDefaultDao, FromMap, FromEnv)
- Core operations (Put, Get, Remove)
- Priority ordering between map and lookup function
- List and map import/export operations
- Lookup function fallback behavior
- Concurrent access patterns

These tests ensure the DAO layer is robust and thread-safe, providing
confidence for refactoring and feature additions.

Signed-off-by: Ulysses Souza <[email protected]>
Increase test coverage for compat/joho/godotenv from 38.1% to 90.7%
by adding 28 tests covering previously untested functions:

- Parse: empty input, quoted values, lookup functions, UTF-8 BOM
- Load: single/multiple files, default filename, missing files, env preservation
- Read: file reading with lookup functions, path resolution
- ReadFile and GetEnvFromFile: error handling and relative paths
- Unmarshal: nil value handling, lookup function integration

This ensures drop-in compatibility with joho/godotenv while using our
improved grammar-based parser under the hood.

Signed-off-by: Ulysses Souza <[email protected]>
Improve both EnvLangFile.g4 and EnvLangValue.g4 grammars to be more
explicit, maintainable, and robust:

**EnvLangFile.g4 improvements:**
- Add explicit COMMENT token for `#` comments
- Replace TEXT with more precise IDENTIFIER and UNQUOTED_VALUE tokens
- IDENTIFIER: strict alphanumeric with underscores, dots, dashes
- UNQUOTED_VALUE: handles spaces and quotes in values
- Separate WS (whitespace) from NEWLINE for better control
- Improve string tokens (DQSTRING, SQSTRING) with escape support
- Note: EXPORT handled in code due to lexer precedence limitations

**EnvLangValue.g4 improvements:**
- Add DOLLAR token for lone `$` signs (fixes edge case)
- Add ESCAPED_CHAR token for `\n`, `\t`, `\r`, `\\`, `\$`, `\"`
- Cleaner TEXT token without catch-all ANY rule
- Better whitespace and newline preservation in multiline values
- More explicit variable substitution patterns

These changes make the grammar self-documenting and eliminate reliance
on the dangerous catch-all ANY rule. The improved token definitions
enable better error messages and more precise parsing.

Signed-off-by: Ulysses Souza <[email protected]>
Regenerate Go parser code from the modernized grammar files using
ANTLR 4.13.1. The generated parsers now expose:

**File parser (EnvLangFile.g4):**
- Type-checking methods: SQSTRING(), DQSTRING(), IDENTIFIER(), UNQUOTED_VALUE()
- Explicit IValueContext and IIdentifierContext interfaces
- Removed deprecated GetStr() method

**Value parser (EnvLangValue.g4):**
- New methods: DOLLAR(), ESCAPED_CHAR()
- Updated IVariableContext with DOLLAR token support
- EscapedChar context for escape sequence handling
- Removed deprecated GetVar_() method

These generated files are the mechanical output of ANTLR and contain
no manual logic changes. The improved API surfaces will be consumed
by the handler updates in the next commit.

Signed-off-by: Ulysses Souza <[email protected]>
Update filehandler.go and valuehandler.go to work with the new parser
API generated from improved ANTLR grammar.

**filehandler.go changes:**
- Replace GetStr().GetTokenType() with type-checking methods
  (SQSTRING(), DQSTRING(), IDENTIFIER(), UNQUOTED_VALUE())
- Use switch statement instead of if-else chain (gocritic)
- Handle export prefix in code (lexer precedence limitation)
- Trim trailing quotes from malformed unquoted values
- Maintain backward compatibility with all existing tests

**valuehandler.go changes:**
- Replace GetVar_() with direct token type checking
  (SIMPLE_VAR(), SIMPLE_STRICT_VAR(), DOLLAR(), etc.)
- Add escape sequence handling in ExitContent:
  \n → newline, \t → tab, \r → carriage return,
  \\ → backslash, \$ → dollar, \" → quote
- Fix lone dollar sign handling to preserve trailing character
- Use switch statement for cleaner control flow (gocritic)
- Update getNameAndDefault signature to accept string instead of token

These changes enable proper escape sequence support and lone dollar
sign handling while maintaining 100% test compatibility (82/82 passing).

Signed-off-by: Ulysses Souza <[email protected]>
Signed-off-by: Ulysses Souza <[email protected]>
Add remaining functions from github.com/joho/godotenv to achieve 100%
API compatibility, making this a true drop-in replacement.

**New functions implemented:**

1. **Overload(filenames ...string) error**
   - Similar to Load but WILL override existing environment variables
   - Updated loadFile() to accept overload parameter

2. **Unmarshal(str string) (map[string]string, error)**
   - Convenience wrapper around UnmarshalWithLookup without lookup function

3. **UnmarshalBytes(src []byte) (map[string]string, error)**
   - Convenience wrapper around UnmarshalBytesWithLookup without lookup function

4. **Marshal(envMap map[string]string) (string, error)**
   - Serializes environment map to dotenv-formatted string
   - Properly escapes special characters in values
   - Each line in format: KEY="value"

5. **Write(envMap map[string]string, filename string) error**
   - Writes serialized environment map to file
   - Uses secure file permissions (0o600)

6. **Exec(filenames []string, cmd string, cmdArgs []string, overload bool) error**
   - Loads env vars from files and executes external command
   - Supports both Load and Overload modes
   - Passes through stdin/stdout/stderr

**Test coverage:**
- Added 14 comprehensive tests in missing_api_test.go
- Tests cover success cases, edge cases, and error handling
- Coverage remains at 90.1% for compat package
- All 96 tests passing (was 82)

**Implementation details:**
- Added os/exec import for command execution
- Added doubleQuoteEscape helper for proper value escaping
- Added convertStringsToBytes helper for Marshal
- Added defaultFileMode constant for security compliance
- Maintains backward compatibility with existing API

The compat/joho/godotenv package now provides complete feature parity
with the original godotenv library, enabling seamless migration for
users who want to benefit from our improved grammar-based parser.

Signed-off-by: Ulysses Souza <[email protected]>
Signed-off-by: Ulysses Souza <[email protected]>
@ulyssessouza ulyssessouza merged commit 18c0b3d into main Nov 10, 2025
1 check 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.

2 participants