1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
use clap::Args;
use std::path::PathBuf;
#[derive(Args)]
pub struct GlobalArgs {
/// The command name to run
pub command_name: String,
/// Arguments to pass to the command
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
pub args: Vec<String>,
}
// ─── Main entry point ────────────────────────────────────────────────────────
pub fn execute(args: &GlobalArgs, cli: &super::Cli) -> anyhow::Result<()> {
use clap::Parser as _;
use std::fs;
let home = composer_home_dir()?;
fs::create_dir_all(&home)?;
if !cli.quiet {
eprintln!("Changed current directory to {}", home.display());
}
// SAFETY: single-threaded at this point; no concurrent env access
unsafe {
std::env::remove_var("COMPOSER");
}
let mut argv: Vec<String> = vec!["mozart".to_string()];
argv.extend(append_global_options(cli));
argv.push("--working-dir".to_string());
argv.push(home.to_string_lossy().into_owned());
argv.push(args.command_name.clone());
argv.extend(args.args.iter().cloned());
let new_cli = super::Cli::try_parse_from(&argv)?;
crate::commands::execute(&new_cli)
}
// ─── Helpers ─────────────────────────────────────────────────────────────────
fn composer_home_dir() -> anyhow::Result<PathBuf> {
if let Ok(val) = std::env::var("COMPOSER_HOME")
&& !val.is_empty()
{
return Ok(PathBuf::from(val));
}
if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME")
&& !xdg.is_empty()
{
return Ok(PathBuf::from(xdg).join("composer"));
}
let home = std::env::var("HOME")
.map(PathBuf::from)
.map_err(|_| anyhow::anyhow!("Cannot determine home directory: $HOME is not set"))?;
Ok(home.join(".config").join("composer"))
}
fn append_global_options(cli: &super::Cli) -> Vec<String> {
let mut opts: Vec<String> = Vec::new();
for _ in 0..cli.verbose {
opts.push("--verbose".to_string());
}
if cli.quiet {
opts.push("--quiet".to_string());
}
if cli.profile {
opts.push("--profile".to_string());
}
if cli.no_plugins {
opts.push("--no-plugins".to_string());
}
if cli.no_scripts {
opts.push("--no-scripts".to_string());
}
if cli.no_cache {
opts.push("--no-cache".to_string());
}
if cli.no_interaction {
opts.push("--no-interaction".to_string());
}
if cli.ansi {
opts.push("--ansi".to_string());
}
if cli.no_ansi {
opts.push("--no-ansi".to_string());
}
opts
}
// ─── Tests ────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
use crate::commands::{Cli, Commands};
use clap::Parser as _;
fn default_cli() -> Cli {
Cli::try_parse_from(["mozart", "about"]).unwrap()
}
// ── composer_home_dir tests ───────────────────────────────────────────────
#[test]
fn test_composer_home_dir_from_env() {
// SAFETY: test-only; single-threaded env mutation
unsafe {
std::env::set_var("COMPOSER_HOME", "/tmp/test-composer-home");
}
let result = composer_home_dir().unwrap();
assert_eq!(result, PathBuf::from("/tmp/test-composer-home"));
// SAFETY: cleanup
unsafe {
std::env::remove_var("COMPOSER_HOME");
}
}
#[test]
fn test_composer_home_dir_xdg() {
// SAFETY: test-only; single-threaded env mutation
unsafe {
std::env::remove_var("COMPOSER_HOME");
std::env::set_var("XDG_CONFIG_HOME", "/tmp/test-xdg-config");
}
let result = composer_home_dir().unwrap();
assert_eq!(result, PathBuf::from("/tmp/test-xdg-config/composer"));
// SAFETY: cleanup
unsafe {
std::env::remove_var("XDG_CONFIG_HOME");
}
}
#[test]
fn test_composer_home_dir_default() {
// SAFETY: test-only; single-threaded env mutation
unsafe {
std::env::remove_var("COMPOSER_HOME");
std::env::remove_var("XDG_CONFIG_HOME");
}
let result = composer_home_dir().unwrap();
let home = std::env::var("HOME").map(PathBuf::from).unwrap();
assert_eq!(result, home.join(".config").join("composer"));
}
// ── append_global_options tests ───────────────────────────────────────────
#[test]
fn test_append_global_options_empty() {
let cli = default_cli();
let opts = append_global_options(&cli);
assert!(opts.is_empty());
}
#[test]
fn test_append_global_options_verbose() {
let cli = Cli::try_parse_from(["mozart", "-vv", "about"]).unwrap();
let opts = append_global_options(&cli);
assert_eq!(opts, vec!["--verbose", "--verbose"]);
}
#[test]
fn test_append_global_options_all() {
let cli = Cli::try_parse_from([
"mozart",
"--verbose",
"--quiet",
"--profile",
"--no-plugins",
"--no-scripts",
"--no-cache",
"--no-interaction",
"--ansi",
"about",
])
.unwrap();
let opts = append_global_options(&cli);
assert!(opts.contains(&"--verbose".to_string()));
assert!(opts.contains(&"--quiet".to_string()));
assert!(opts.contains(&"--profile".to_string()));
assert!(opts.contains(&"--no-plugins".to_string()));
assert!(opts.contains(&"--no-scripts".to_string()));
assert!(opts.contains(&"--no-cache".to_string()));
assert!(opts.contains(&"--no-interaction".to_string()));
assert!(opts.contains(&"--ansi".to_string()));
}
#[test]
fn test_append_global_options_does_not_forward_working_dir() {
let cli = Cli::try_parse_from(["mozart", "--working-dir", "/some/path", "about"]).unwrap();
let opts = append_global_options(&cli);
assert!(!opts.iter().any(|o| o.contains("working-dir")));
assert!(!opts.iter().any(|o| o == "/some/path"));
}
#[test]
fn test_global_args_has_correct_command() {
// Verify GlobalArgs parses correctly through the CLI
let cli = Cli::try_parse_from(["mozart", "global", "require", "vendor/package"]).unwrap();
if let Commands::Global(args) = cli.command {
assert_eq!(args.command_name, "require");
assert_eq!(args.args, vec!["vendor/package"]);
} else {
panic!("Expected Global command");
}
}
#[test]
fn test_global_args_hyphen_values() {
// Verify hyphen values in trailing args are accepted
let cli = Cli::try_parse_from(["mozart", "global", "require", "vendor/pkg", "--no-update"])
.unwrap();
if let Commands::Global(args) = cli.command {
assert_eq!(args.command_name, "require");
assert!(args.args.contains(&"--no-update".to_string()));
} else {
panic!("Expected Global command");
}
}
}
|