kopi 0.0.6

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
429
430
431
432
433
434
435
// Copyright 2025 dentsusoken
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::config::KopiConfig;
use crate::error::{KopiError, Result};
use crate::version::VersionRequest;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;

const KOPI_VERSION_FILE: &str = ".kopi-version";
const JAVA_VERSION_FILE: &str = ".java-version";
const VERSION_ENV_VAR: &str = "KOPI_JAVA_VERSION";

// Type alias to simplify complex return type
type VersionSearchResult = (Option<(VersionRequest, PathBuf)>, Vec<String>);

#[derive(Debug, Clone, PartialEq)]
pub enum VersionSource {
    Environment(String),    // KOPI_JAVA_VERSION
    ProjectFile(PathBuf),   // .kopi-version or .java-version
    GlobalDefault(PathBuf), // ~/.kopi/default-version
}

pub struct VersionResolver<'a> {
    current_dir: PathBuf,
    config: &'a KopiConfig,
}

impl<'a> VersionResolver<'a> {
    pub fn new(config: &'a KopiConfig) -> Self {
        Self {
            current_dir: env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
            config,
        }
    }

    pub fn with_dir(dir: PathBuf, config: &'a KopiConfig) -> Self {
        Self {
            current_dir: dir,
            config,
        }
    }

    pub fn resolve_version(&self) -> Result<(VersionRequest, VersionSource)> {
        // Check environment variable first (fastest)
        if let Ok(env_version) = env::var(VERSION_ENV_VAR) {
            log::debug!("Checking KOPI_JAVA_VERSION environment variable...");
            log::debug!("Found KOPI_JAVA_VERSION: {env_version}");
            let version_request = VersionRequest::from_str(&env_version)?;
            return Ok((version_request, VersionSource::Environment(env_version)));
        }
        log::debug!("KOPI_JAVA_VERSION not set");

        // Search for version files
        let current_dir = self.current_dir.clone();
        log::debug!("Searching for version files from: {current_dir:?}");

        let (version_request, searched_paths) = self.search_version_files()?;
        if let Some((version_request, path)) = version_request {
            return Ok((version_request, VersionSource::ProjectFile(path)));
        }

        // Check global default
        if let Some((version_request, path)) = self.get_global_default()? {
            log::debug!("Using global default version");
            return Ok((version_request, VersionSource::GlobalDefault(path)));
        }

        // No version found - use the searched paths from search_version_files
        Err(KopiError::NoLocalVersion { searched_paths })
    }

    fn read_version_file(&self, path: &Path) -> Result<String> {
        // Use a small buffer for efficiency
        let content = fs::read_to_string(path)?;

        // Trim whitespace and newlines
        let version = content.trim().to_string();

        if version.is_empty() {
            return Err(KopiError::InvalidVersionFormat(
                "Version file is empty".to_string(),
            ));
        }

        Ok(version)
    }

    fn search_version_files(&self) -> Result<VersionSearchResult> {
        let mut current = self.current_dir.clone();
        let mut searched_paths = Vec::new();

        loop {
            // Add current directory to searched paths
            searched_paths.push(current.display().to_string());

            // Check for .kopi-version first (native format)
            let kopi_version_path = current.join(KOPI_VERSION_FILE);
            log::trace!("Checking {kopi_version_path:?}");
            if kopi_version_path.exists() {
                log::debug!("Found .kopi-version at {kopi_version_path:?}");
                let content = self.read_version_file(&kopi_version_path)?;
                log::debug!("Version content: {content}");
                let version_request = VersionRequest::from_str(&content)?;
                return Ok((Some((version_request, kopi_version_path)), searched_paths));
            }

            // Check for .java-version (compatibility)
            let java_version_path = current.join(JAVA_VERSION_FILE);
            log::trace!("Checking {java_version_path:?}");
            if java_version_path.exists() {
                log::debug!("Found .java-version at {java_version_path:?}");
                let content = self.read_version_file(&java_version_path)?;
                log::debug!("Version content: {content}");
                // .java-version doesn't support distribution@version format
                let version_request = VersionRequest::new(content)?;
                return Ok((Some((version_request, java_version_path)), searched_paths));
            }

            // Move to parent directory
            match current.parent() {
                Some(parent) => current = parent.to_path_buf(),
                None => break,
            }
        }

        Ok((None, searched_paths))
    }

    fn get_global_default(&self) -> Result<Option<(VersionRequest, PathBuf)>> {
        let global_version_path = self.config.kopi_home().join("version");

        if global_version_path.exists() {
            let content = self.read_version_file(&global_version_path)?;
            let version_request = VersionRequest::from_str(&content)?;
            return Ok(Some((version_request, global_version_path)));
        }

        Ok(None)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::KopiConfig;
    use serial_test::serial;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    #[serial]
    fn test_resolve_from_env_var() {
        unsafe {
            env::set_var(VERSION_ENV_VAR, "temurin@21");
        }
        let temp_dir = TempDir::new().unwrap();
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::new(&config);
        let (result, source) = resolver.resolve_version().unwrap();
        assert_eq!(result.version_pattern, "21");
        assert_eq!(result.distribution, Some("temurin".to_string()));
        assert_eq!(source, VersionSource::Environment("temurin@21".to_string()));
        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }
    }

    #[test]
    #[serial]
    fn test_resolve_from_kopi_version_file() {
        // Clear environment variable to ensure test isolation
        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }

        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();

        let version_file = temp_path.join(KOPI_VERSION_FILE);
        fs::write(&version_file, "corretto@17.0.8").unwrap();

        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::with_dir(temp_path.clone(), &config);
        let (result, source) = resolver.resolve_version().unwrap();
        assert_eq!(result.version_pattern, "17.0.8");
        assert_eq!(result.distribution, Some("corretto".to_string()));
        assert_eq!(source, VersionSource::ProjectFile(version_file));
    }

    #[test]
    #[serial]
    fn test_resolve_from_java_version_file() {
        // Clear environment variable to ensure test isolation
        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }

        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();

        let version_file = temp_path.join(JAVA_VERSION_FILE);
        fs::write(&version_file, "11.0.2").unwrap();

        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::with_dir(temp_path.clone(), &config);
        let (result, source) = resolver.resolve_version().unwrap();
        assert_eq!(result.version_pattern, "11.0.2");
        assert_eq!(result.distribution, None);
        assert_eq!(source, VersionSource::ProjectFile(version_file));
    }

    #[test]
    #[serial]
    fn test_resolve_searches_parent_directories() {
        // Clear environment variable to ensure test isolation
        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }

        let temp_dir = TempDir::new().unwrap();
        let parent_dir = temp_dir.path().to_path_buf();

        let child_dir = parent_dir.join("child");
        fs::create_dir_all(&child_dir).unwrap();

        // Place version file in parent
        let version_file = parent_dir.join(KOPI_VERSION_FILE);
        fs::write(&version_file, "zulu@8").unwrap();

        // Resolver starts in child directory
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::with_dir(child_dir, &config);
        let (result, source) = resolver.resolve_version().unwrap();
        assert_eq!(result.version_pattern, "8");
        assert_eq!(result.distribution, Some("zulu".to_string()));
        assert_eq!(source, VersionSource::ProjectFile(version_file));
    }

    #[test]
    #[serial]
    fn test_kopi_version_takes_precedence() {
        // Save original value and ensure clean state
        let original = env::var(VERSION_ENV_VAR).ok();
        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }

        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();

        // Create both version files
        let kopi_version = temp_path.join(KOPI_VERSION_FILE);
        fs::write(&kopi_version, "temurin@21").unwrap();

        let java_version = temp_path.join(JAVA_VERSION_FILE);
        fs::write(&java_version, "17").unwrap();

        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::with_dir(temp_path.clone(), &config);
        let (result, source) = resolver.resolve_version().unwrap();

        // Should use .kopi-version
        assert_eq!(result.version_pattern, "21");
        assert_eq!(result.distribution, Some("temurin".to_string()));
        assert_eq!(source, VersionSource::ProjectFile(kopi_version));

        // Restore original value
        unsafe {
            if let Some(val) = original {
                env::set_var(VERSION_ENV_VAR, val);
            } else {
                env::remove_var(VERSION_ENV_VAR);
            }
        }
    }

    #[test]
    #[serial]
    fn test_empty_version_file_error() {
        // Clear environment variable to ensure test isolation
        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }

        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();

        let version_file = temp_path.join(KOPI_VERSION_FILE);
        fs::write(&version_file, "").unwrap();

        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::with_dir(temp_path.clone(), &config);
        let result = resolver.resolve_version();
        assert!(result.is_err());
    }

    #[test]
    #[serial]
    fn test_whitespace_trimmed() {
        // Clear environment variable to ensure test isolation
        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }

        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();

        let version_file = temp_path.join(JAVA_VERSION_FILE);
        fs::write(&version_file, "  17.0.9  \n").unwrap();

        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::with_dir(temp_path.clone(), &config);
        let (result, _source) = resolver.resolve_version().unwrap();
        assert_eq!(result.version_pattern, "17.0.9");
    }

    #[test]
    #[serial]
    fn test_no_version_found() {
        // Clear environment variable to ensure test isolation
        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }

        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();

        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::with_dir(temp_path.clone(), &config);
        let result = resolver.resolve_version();
        assert!(matches!(result, Err(KopiError::NoLocalVersion { .. })));
    }

    #[test]
    #[serial]
    fn test_resolve_from_global_when_exists() {
        // Clear environment variable to ensure test isolation
        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }

        // Test when global version file exists
        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();

        // Create a global version file in the temp kopi home
        let global_version_path = temp_dir.path().join("version");
        fs::write(&global_version_path, "temurin@17").unwrap();

        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::with_dir(temp_path.clone(), &config);
        let result = resolver.resolve_version();

        // Should successfully resolve from global default
        let (version_request, source) = result.unwrap();
        assert_eq!(version_request.version_pattern, "17");
        assert_eq!(version_request.distribution, Some("temurin".to_string()));
        assert_eq!(source, VersionSource::GlobalDefault(global_version_path));
    }

    #[test]
    #[serial]
    fn test_resolve_from_global_when_not_exists() {
        // Clear environment variable to ensure test isolation
        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }

        // Test when no version files exist anywhere
        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();

        // Don't create any version files
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::with_dir(temp_path.clone(), &config);
        let result = resolver.resolve_version();

        // Should return NoLocalVersion error
        assert!(matches!(result, Err(KopiError::NoLocalVersion { .. })));

        // Verify the error contains searched paths
        if let Err(KopiError::NoLocalVersion { searched_paths }) = result {
            assert!(!searched_paths.is_empty());
            assert!(
                searched_paths
                    .iter()
                    .any(|p| p.contains(&temp_path.display().to_string()))
            );
        }
    }

    #[test]
    #[serial]
    fn test_resolve_priority() {
        // Test that environment variable takes priority over project files
        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();

        // Create project file
        let version_file = temp_path.join(KOPI_VERSION_FILE);
        fs::write(&version_file, "corretto@17").unwrap();

        // Set environment variable
        unsafe {
            env::set_var(VERSION_ENV_VAR, "temurin@21");
        }

        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let resolver = VersionResolver::with_dir(temp_path.clone(), &config);
        let (version_request, source) = resolver.resolve_version().unwrap();

        // Should use environment variable, not project file
        assert_eq!(version_request.version_pattern, "21");
        assert_eq!(version_request.distribution, Some("temurin".to_string()));
        assert_eq!(source, VersionSource::Environment("temurin@21".to_string()));

        unsafe {
            env::remove_var(VERSION_ENV_VAR);
        }
    }
}