|  | 
|  | 1 | +// | 
|  | 2 | +// Copyright (c) 2024 Ian McLinden | 
|  | 3 | +// | 
|  | 4 | +// This file is part of the posixutils-rs project covered under | 
|  | 5 | +// the MIT License.  For the full license text, please see the LICENSE | 
|  | 6 | +// file in the root directory of this project. | 
|  | 7 | +// SPDX-License-Identifier: MIT | 
|  | 8 | +// | 
|  | 9 | + | 
|  | 10 | +use plib::{run_test, TestPlan}; | 
|  | 11 | + | 
|  | 12 | +fn realpath_test(args: &[&str], output: &str, expected_code: i32) { | 
|  | 13 | +    let str_args: Vec<String> = args.iter().map(|s| String::from(*s)).collect(); | 
|  | 14 | + | 
|  | 15 | +    run_test(TestPlan { | 
|  | 16 | +        cmd: String::from("realpath"), | 
|  | 17 | +        args: str_args, | 
|  | 18 | +        stdin_data: String::new(), | 
|  | 19 | +        expected_out: output.to_string(), | 
|  | 20 | +        expected_err: String::new(), | 
|  | 21 | +        expected_exit_code: expected_code, | 
|  | 22 | +    }); | 
|  | 23 | +} | 
|  | 24 | + | 
|  | 25 | +#[test] | 
|  | 26 | +fn realpath_current_directory() { | 
|  | 27 | +    let curr_dir = std::env::current_dir().unwrap(); | 
|  | 28 | +    let out_str = &format!("{}\n", curr_dir.to_str().unwrap()); | 
|  | 29 | + | 
|  | 30 | +    realpath_test(&["."], out_str, 0); | 
|  | 31 | +    realpath_test(&["./"], out_str, 0); | 
|  | 32 | +    realpath_test(&["./."], out_str, 0); | 
|  | 33 | +} | 
|  | 34 | + | 
|  | 35 | +#[test] | 
|  | 36 | +fn realpath_parent_directory() { | 
|  | 37 | +    let curr_dir = std::env::current_dir().unwrap(); | 
|  | 38 | +    let parent_dir = curr_dir.parent().unwrap(); | 
|  | 39 | +    let out_str = format!("{}\n", parent_dir.to_str().unwrap()); | 
|  | 40 | + | 
|  | 41 | +    realpath_test(&[".."], &out_str, 0); | 
|  | 42 | +    realpath_test(&["../"], &out_str, 0); | 
|  | 43 | +    realpath_test(&["../."], &out_str, 0); | 
|  | 44 | +    realpath_test(&["./.."], &out_str, 0); | 
|  | 45 | +    realpath_test(&["./../"], &out_str, 0); | 
|  | 46 | +    realpath_test(&["./../."], &out_str, 0); | 
|  | 47 | +} | 
|  | 48 | + | 
|  | 49 | +#[test] | 
|  | 50 | +fn realpath_endless_parent() { | 
|  | 51 | +    realpath_test( | 
|  | 52 | +        &["../../../../../../../../../../../../../../../../../../../.."], | 
|  | 53 | +        "/\n", | 
|  | 54 | +        0, | 
|  | 55 | +    ); | 
|  | 56 | +    realpath_test( | 
|  | 57 | +        &["../../../../../../../../../../../../../../../../../../../../"], | 
|  | 58 | +        "/\n", | 
|  | 59 | +        0, | 
|  | 60 | +    ); | 
|  | 61 | +    realpath_test( | 
|  | 62 | +        &["../../../../../../../../../../../../../../../../../../../../."], | 
|  | 63 | +        "/\n", | 
|  | 64 | +        0, | 
|  | 65 | +    ); | 
|  | 66 | +    realpath_test( | 
|  | 67 | +        &["./../../../../../../../../../../../../../../../../../../../.."], | 
|  | 68 | +        "/\n", | 
|  | 69 | +        0, | 
|  | 70 | +    ); | 
|  | 71 | +    realpath_test( | 
|  | 72 | +        &["./../../../../../../../../../../../../../../../../../../../../"], | 
|  | 73 | +        "/\n", | 
|  | 74 | +        0, | 
|  | 75 | +    ); | 
|  | 76 | +    realpath_test( | 
|  | 77 | +        &["./../../../../../../../../../../../../../../../../../../../../."], | 
|  | 78 | +        "/\n", | 
|  | 79 | +        0, | 
|  | 80 | +    ); | 
|  | 81 | +} | 
|  | 82 | + | 
|  | 83 | +#[test] | 
|  | 84 | +fn realpath_relative_directory() { | 
|  | 85 | +    let curr_dir = std::env::current_dir().unwrap(); | 
|  | 86 | +    let curr_dir_name = curr_dir.file_name().unwrap(); | 
|  | 87 | +    let out_str = &format!("{}\n", curr_dir.to_str().unwrap()); | 
|  | 88 | + | 
|  | 89 | +    let in_str = &format!("../{}", curr_dir_name.to_str().unwrap()); | 
|  | 90 | +    realpath_test(&[in_str], out_str, 0); | 
|  | 91 | + | 
|  | 92 | +    let in_str = &format!("./../{}", curr_dir_name.to_str().unwrap()); | 
|  | 93 | +    realpath_test(&[in_str], out_str, 0); | 
|  | 94 | + | 
|  | 95 | +    let in_str = &format!( | 
|  | 96 | +        "{}/../{}", | 
|  | 97 | +        curr_dir.to_str().unwrap(), | 
|  | 98 | +        curr_dir_name.to_str().unwrap() | 
|  | 99 | +    ); | 
|  | 100 | +    realpath_test(&[in_str], out_str, 0); | 
|  | 101 | +} | 
|  | 102 | + | 
|  | 103 | +#[test] | 
|  | 104 | +fn realpath_multiple_args() { | 
|  | 105 | +    let curr_dir = std::env::current_dir().unwrap(); | 
|  | 106 | +    let parent_dir = curr_dir.parent().unwrap(); | 
|  | 107 | +    let out_str = &format!( | 
|  | 108 | +        "{}\n{}\n/\n", | 
|  | 109 | +        curr_dir.to_str().unwrap(), | 
|  | 110 | +        parent_dir.to_str().unwrap() | 
|  | 111 | +    ); | 
|  | 112 | +    realpath_test(&[".", "..", "/"], out_str, 0); | 
|  | 113 | +} | 
|  | 114 | + | 
|  | 115 | +#[test] | 
|  | 116 | +fn realpath_no_args() { | 
|  | 117 | +    let curr_dir = std::env::current_dir().unwrap(); | 
|  | 118 | +    let out_str = format!("{}\n", curr_dir.to_str().unwrap()); | 
|  | 119 | + | 
|  | 120 | +    realpath_test(&[], &out_str, 0); | 
|  | 121 | +} | 
|  | 122 | + | 
|  | 123 | +#[test] | 
|  | 124 | +fn realpath_empty_path() { | 
|  | 125 | +    run_test(TestPlan { | 
|  | 126 | +        cmd: String::from("realpath"), | 
|  | 127 | +        args: vec![String::new()], | 
|  | 128 | +        stdin_data: String::new(), | 
|  | 129 | +        expected_out: String::new(), | 
|  | 130 | +        expected_err: String::from("\"\": No such file or directory (os error 2)\n"), | 
|  | 131 | +        expected_exit_code: 1, | 
|  | 132 | +    }); | 
|  | 133 | +} | 
0 commit comments