kopi 0.0.1

Kopi is a JDK version management tool
Documentation
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
mod common;
use assert_cmd::Command;
use common::TestHomeGuard;
use predicates::prelude::*;
use serial_test::serial;
use std::fs;
use std::path::Path;

fn get_test_command(kopi_home: &Path) -> Command {
    let mut cmd = Command::cargo_bin("kopi").unwrap();
    cmd.env("KOPI_HOME", kopi_home.to_str().unwrap());
    cmd.env("HOME", kopi_home.parent().unwrap());
    // Change to home directory to avoid picking up project's .kopi-version
    cmd.current_dir(kopi_home.parent().unwrap());
    // Clear KOPI_JAVA_VERSION to prevent it from overriding test setups
    cmd.env_remove("KOPI_JAVA_VERSION");
    cmd
}

/// Test basic env command with bash shell (default)
#[test]
#[serial]
fn test_env_basic_bash() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a mock JDK installation
    let jdk_path = kopi_home.join("jdks").join("temurin-21.0.1");
    fs::create_dir_all(&jdk_path).unwrap();

    // Set a global version
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "temurin@21.0.1").unwrap();

    // Test env command
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env").arg("--shell").arg("bash");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("export JAVA_HOME="))
        .stdout(predicate::str::contains("temurin-21.0.1"));
}

/// Test env command with fish shell
#[test]
#[serial]
fn test_env_fish_shell() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a mock JDK installation
    let jdk_path = kopi_home.join("jdks").join("corretto-17.0.5");
    fs::create_dir_all(&jdk_path).unwrap();

    // Set a global version
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "corretto@17.0.5").unwrap();

    // Test env command with fish shell
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env").arg("--shell").arg("fish");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("set -gx JAVA_HOME"))
        .stdout(predicate::str::contains("corretto-17.0.5"));
}

/// Test env command with PowerShell
#[test]
#[serial]
fn test_env_powershell() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a mock JDK installation
    let jdk_path = kopi_home.join("jdks").join("zulu-11.0.20");
    fs::create_dir_all(&jdk_path).unwrap();

    // Set a global version
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "zulu@11.0.20").unwrap();

    // Test env command with PowerShell
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env").arg("--shell").arg("powershell");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("$env:JAVA_HOME ="))
        .stdout(predicate::str::contains("zulu-11.0.20"));
}

/// Test env command with Windows CMD
#[test]
#[serial]
fn test_env_cmd() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a mock JDK installation
    let jdk_path = kopi_home.join("jdks").join("microsoft-21.0.1");
    fs::create_dir_all(&jdk_path).unwrap();

    // Set a global version
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "microsoft@21.0.1").unwrap();

    // Test env command with CMD
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env").arg("--shell").arg("cmd");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("set JAVA_HOME="))
        .stdout(predicate::str::contains("microsoft-21.0.1"));
}

/// Test env command without export flag
#[test]
#[serial]
fn test_env_no_export() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a mock JDK installation
    let jdk_path = kopi_home.join("jdks").join("temurin-17.0.8");
    fs::create_dir_all(&jdk_path).unwrap();

    // Set a global version
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "temurin@17.0.8").unwrap();

    // Test env command without export
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env")
        .arg("--shell")
        .arg("bash")
        .arg("--export")
        .arg("false");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("JAVA_HOME="))
        .stdout(predicate::str::contains("temurin-17.0.8"))
        .stdout(predicate::str::contains("export").not());
}

/// Test env command with explicit version
#[test]
#[serial]
fn test_env_explicit_version() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create multiple JDK installations
    let jdk17_path = kopi_home.join("jdks").join("temurin-17.0.8");
    let jdk21_path = kopi_home.join("jdks").join("temurin-21.0.1");
    fs::create_dir_all(&jdk17_path).unwrap();
    fs::create_dir_all(&jdk21_path).unwrap();

    // Set global version to 17
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "temurin@17.0.8").unwrap();

    // Test env command with explicit version (should use 21)
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env")
        .arg("--shell")
        .arg("bash")
        .arg("temurin@21.0.1");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("export JAVA_HOME="))
        .stdout(predicate::str::contains("temurin-21.0.1"));
}

/// Test env command with .kopi-version file
#[test]
#[serial]
fn test_env_kopi_version_file() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a mock JDK installation
    let jdk_path = kopi_home.join("jdks").join("graalvm-22.0.1");
    fs::create_dir_all(&jdk_path).unwrap();

    // Create a project directory with .kopi-version
    let project_dir = test_home.path().join("project");
    fs::create_dir_all(&project_dir).unwrap();
    fs::write(project_dir.join(".kopi-version"), "graalvm@22.0.1").unwrap();

    // Test env command from project directory
    let mut cmd = get_test_command(&kopi_home);
    cmd.current_dir(&project_dir);
    cmd.arg("env").arg("--shell").arg("bash");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("export JAVA_HOME="))
        .stdout(predicate::str::contains("graalvm-22.0.1"));
}

/// Test env command with JDK not installed
#[test]
#[serial]
fn test_env_jdk_not_installed() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Set a global version without installing the JDK
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "temurin@21.0.1").unwrap();

    // Test env command
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env").arg("--shell").arg("bash");
    cmd.assert().failure().stderr(predicate::str::contains(
        "JDK 'temurin@21.0.1' is not installed",
    ));
}

/// Test env command with no version configured
#[test]
#[serial]
fn test_env_no_version() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Test env command without any version configured
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env").arg("--shell").arg("bash");
    cmd.assert().failure().stderr(predicate::str::contains(
        "No JDK configured for current project",
    ));
}

/// Test env command with path containing spaces
#[test]
#[serial]
fn test_env_path_with_spaces() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a JDK path with spaces (simulated)
    let jdk_path = kopi_home.join("jdks").join("temurin with spaces-21.0.1");
    fs::create_dir_all(&jdk_path).unwrap();

    // Set a global version
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "temurin with spaces@21.0.1").unwrap();

    // Test env command handles spaces correctly
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env").arg("--shell").arg("bash");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("export JAVA_HOME="))
        .stdout(predicate::str::contains("temurin with spaces-21.0.1"));
}

/// Test env command with invalid shell name
#[test]
#[serial]
fn test_env_invalid_shell() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a mock JDK installation
    let jdk_path = kopi_home.join("jdks").join("temurin-21.0.1");
    fs::create_dir_all(&jdk_path).unwrap();

    // Set a global version so we don't get "No JDK configured" error
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "temurin@21.0.1").unwrap();

    // Test env command with invalid shell
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env").arg("--shell").arg("invalid-shell");
    cmd.assert().failure().stderr(predicate::str::contains(
        "Shell 'invalid-shell' is not supported",
    ));
}

/// Test shell auto-detection fallback
#[test]
#[serial]
fn test_env_shell_detection() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a mock JDK installation
    let jdk_path = kopi_home.join("jdks").join("temurin-21.0.1");
    fs::create_dir_all(&jdk_path).unwrap();

    // Set a global version
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "temurin@21.0.1").unwrap();

    // Test env command with explicit shell arguments
    let shells = vec![
        ("bash", "export JAVA_HOME="),
        ("zsh", "export JAVA_HOME="),
        ("powershell", "$env:JAVA_HOME ="),
        ("cmd", "set JAVA_HOME"),
    ];

    for (shell_name, expected_prefix) in shells {
        let mut cmd = get_test_command(&kopi_home);
        cmd.arg("env").arg("--shell").arg(shell_name);
        cmd.assert()
            .success()
            .stdout(predicate::str::contains(expected_prefix));
    }
}

/// Test env command with malformed version file
#[test]
#[serial]
fn test_env_malformed_version_file() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a malformed global version file
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "invalid@@version").unwrap();

    // Test env command
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env").arg("--shell").arg("bash");
    cmd.assert().failure().stderr(predicate::str::contains(
        "Invalid configuration: Unknown package type: invalid",
    ));
}

/// Test env command with environment variable override
#[test]
#[serial]
fn test_env_with_kopi_java_version() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create two JDK installations
    let jdk17_path = kopi_home.join("jdks").join("temurin-17.0.8");
    let jdk21_path = kopi_home.join("jdks").join("temurin-21.0.1");
    fs::create_dir_all(&jdk17_path).unwrap();
    fs::create_dir_all(&jdk21_path).unwrap();

    // Set global version to 17
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "temurin@17.0.8").unwrap();

    // Test with KOPI_JAVA_VERSION environment variable (should override global)
    let mut cmd = get_test_command(&kopi_home);
    cmd.env("KOPI_JAVA_VERSION", "temurin@21.0.1");
    cmd.arg("env").arg("--shell").arg("bash");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("export JAVA_HOME="))
        .stdout(predicate::str::contains("temurin-21.0.1"));
}

/// Test env command with missing parent directories
#[test]
#[serial]
fn test_env_version_resolution_hierarchy() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create a JDK installation
    let jdk_path = kopi_home.join("jdks").join("zulu-11.0.20");
    fs::create_dir_all(&jdk_path).unwrap();

    // Create nested project structure
    let project_root = test_home.path().join("project");
    let sub_dir = project_root.join("src").join("main").join("java");
    fs::create_dir_all(&sub_dir).unwrap();

    // Create .kopi-version in project root
    fs::write(project_root.join(".kopi-version"), "zulu@11.0.20").unwrap();

    // Test from subdirectory (should find parent .kopi-version)
    let mut cmd = get_test_command(&kopi_home);
    cmd.current_dir(&sub_dir);
    cmd.arg("env").arg("--shell").arg("bash");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("export JAVA_HOME="))
        .stdout(predicate::str::contains("zulu-11.0.20"));
}

/// Test env command with multiple matching JDKs (should use latest)
#[test]
#[serial]
fn test_env_multiple_matching_jdks() {
    let test_home = TestHomeGuard::new();
    test_home.setup_kopi_structure();
    let kopi_home = test_home.kopi_home();

    // Create multiple JDK installations with same major version
    let jdk_old_path = kopi_home.join("jdks").join("temurin-21.0.1");
    let jdk_new_path = kopi_home.join("jdks").join("temurin-21.0.2");
    fs::create_dir_all(&jdk_old_path).unwrap();
    fs::create_dir_all(&jdk_new_path).unwrap();

    // Set a global version with just major version
    let global_version_file = kopi_home.join("version");
    fs::write(&global_version_file, "temurin@21").unwrap();

    // Test env command (should use latest version)
    let mut cmd = get_test_command(&kopi_home);
    cmd.arg("env").arg("--shell").arg("bash");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("export JAVA_HOME="))
        .stdout(predicate::str::contains("temurin-21.0.2"));
}