Windows平台下无法在.kitchen.yml中使用ENV变量的问题求助
I’ve run into this exact cross-platform quirk with Test Kitchen before, and the root issue boils down to Windows vs. Linux environment variable differences plus how Test Kitchen parses ERB in your .kitchen.yml on Windows.
Problem Breakdown
Let’s unpack why this works on Ubuntu but fails on Windows:
- On Linux/Unix systems,
PWDis a standard environment variable that holds the current working directory. - On Windows, there is no native
PWDenvironment variable—Windows usesCDinstead. When you referenceENV['PWD']in your ERB, it returnsnilon Windows, which breaks YAML parsing entirely, leading to thatKitchen::UserErrorabout invalid YAML. - The cucumber/aruba issue you’re thinking of is likely related to the same core problem: cross-platform environment variable inconsistencies breaking template parsing.
Solutions
Here are three reliable fixes, ordered by robustness:
1. Use Ruby’s Cross-Platform Dir.pwd (Best Option)
Instead of relying on system-specific environment variables, use Ruby’s built-in Dir.pwd method—it automatically returns the correct current working directory regardless of OS, and handles path separators properly.
Update your synced_folders line to:
synced_folders: - ["<%= Dir.pwd %>", "/target/path/in/vm"]
2. Use Windows’ Native CD Environment Variable
If you prefer sticking to environment variables, replace ENV['PWD'] with ENV['CD'] (Windows' equivalent):
synced_folders: - ["<%= ENV['CD'] %>", "/target/path/in/vm"]
Note: This will break compatibility with Linux systems, so only use this if you’re exclusively running on Windows.
3. Manually Set PWD in Your Windows Terminal
If you need to keep the original ENV['PWD'] syntax for cross-platform compatibility, manually set the PWD variable before running Test Kitchen commands:
For Command Prompt:
set PWD=%CD% kitchen list
For PowerShell:
$env:PWD = Get-Location kitchen list
Verification Steps
- Save your modified
.kitchen.yml - Open a fresh terminal (to ensure environment variables are properly loaded)
- Run
kitchen list—it should now parse the YAML correctly without throwing theKitchen::UserError
内容的提问来源于stack exchange,提问作者mdo123

