| 1 | module os |
| 2 | |
| 3 | fn test_is_abs_path() { |
| 4 | $if windows { |
| 5 | assert is_abs_path('/') |
| 6 | assert is_abs_path('\\') |
| 7 | assert !is_abs_path('\\\\') |
| 8 | assert is_abs_path(r'C:\path\to\files\file.v') |
| 9 | assert is_abs_path(r'\\Host\share') |
| 10 | assert is_abs_path(r'//Host\share\files\file.v') |
| 11 | assert is_abs_path(r'\\.\BootPartition\Windows') |
| 12 | assert !is_abs_path(r'\\.\') |
| 13 | assert !is_abs_path(r'\\?\\') |
| 14 | assert !is_abs_path(r'C:path\to\dir') |
| 15 | assert !is_abs_path(r'dir') |
| 16 | assert !is_abs_path(r'.\') |
| 17 | assert !is_abs_path(r'.') |
| 18 | assert !is_abs_path(r'\\Host') |
| 19 | assert !is_abs_path(r'\\Host\') |
| 20 | return |
| 21 | } |
| 22 | assert is_abs_path('/') |
| 23 | assert is_abs_path('/path/to/files/file.v') |
| 24 | assert !is_abs_path('\\') |
| 25 | assert !is_abs_path('path/to/files/file.v') |
| 26 | assert !is_abs_path('dir') |
| 27 | assert !is_abs_path('./') |
| 28 | assert !is_abs_path('.') |
| 29 | } |
| 30 | |
| 31 | fn test_clean_path() { |
| 32 | $if windows { |
| 33 | assert clean_path(r'\\path\to\files/file.v') == r'\path\to\files\file.v' |
| 34 | assert clean_path(r'\/\//\/') == '\\' |
| 35 | assert clean_path(r'./path\\dir/\\./\/\\/file.v\.\\\.') == r'path\dir\file.v' |
| 36 | assert clean_path(r'\./path/dir\\file.exe') == r'\path\dir\file.exe' |
| 37 | assert clean_path(r'.') == '' |
| 38 | assert clean_path(r'./') == '' |
| 39 | assert clean_path('') == '' |
| 40 | assert clean_path(r'\./') == '\\' |
| 41 | assert clean_path(r'//\/\/////') == '\\' |
| 42 | return |
| 43 | } |
| 44 | assert clean_path('./../.././././//') == '../..' |
| 45 | assert clean_path('') == '' |
| 46 | assert clean_path('.') == '' |
| 47 | assert clean_path('./path/to/file.v//./') == 'path/to/file.v' |
| 48 | assert clean_path('./') == '' |
| 49 | assert clean_path('/.') == '/' |
| 50 | assert clean_path('//path/./to/.///files/file.v///') == '/path/to/files/file.v' |
| 51 | assert clean_path('path/./to/.///files/.././file.v///') == 'path/to/files/../file.v' |
| 52 | assert clean_path('\\') == '\\' |
| 53 | assert clean_path('//////////') == '/' |
| 54 | } |
| 55 | |
| 56 | fn test_to_slash() { |
| 57 | sep := path_separator |
| 58 | assert to_slash('') == '' |
| 59 | assert to_slash(sep) == ('/') |
| 60 | assert to_slash([sep, 'a', sep, 'b'].join('')) == '/a/b' |
| 61 | assert to_slash(['a', sep, sep, 'b'].join('')) == 'a//b' |
| 62 | } |
| 63 | |
| 64 | fn test_from_slash() { |
| 65 | sep := path_separator |
| 66 | assert from_slash('') == '' |
| 67 | assert from_slash('/') == sep |
| 68 | assert from_slash('/a/b') == [sep, 'a', sep, 'b'].join('') |
| 69 | assert from_slash('a//b') == ['a', sep, sep, 'b'].join('') |
| 70 | } |
| 71 | |
| 72 | fn test_norm_path() { |
| 73 | $if windows { |
| 74 | assert norm_path(r'C:/path/to//file.v\\') == r'C:\path\to\file.v' |
| 75 | assert norm_path(r'C:path\.\..\\\.\to//file.v') == r'C:to\file.v' |
| 76 | assert norm_path(r'D:path\.\..\..\\\\.\to//dir/..\') == r'D:..\to' |
| 77 | assert norm_path(r'D:/path\.\..\/..\file.v') == r'D:\file.v' |
| 78 | assert norm_path(r'') == '.' |
| 79 | assert norm_path(r'/') == '\\' |
| 80 | assert norm_path(r'\/') == '\\' |
| 81 | assert norm_path(r'path\../dir\..') == '.' |
| 82 | assert norm_path(r'.\.\') == '.' |
| 83 | assert norm_path(r'G:.\.\dir\././\.\.\\\\///to/././\file.v/./\\') == r'G:dir\to\file.v' |
| 84 | assert norm_path(r'G:\..\..\.\.\file.v\\\.') == r'G:\file.v' |
| 85 | assert norm_path(r'\\Server\share\\\dir/..\file.v\./.') == r'\\Server\share\file.v' |
| 86 | assert norm_path(r'\\.\device\\\dir/to/./file.v\.') == r'\\.\device\dir\to\file.v' |
| 87 | assert norm_path(r'C:dir/../dir2/../../../file.v') == r'C:..\..\file.v' |
| 88 | assert norm_path(r'\\.\C:\\\Users/\Documents//..') == r'\\.\C:\Users' |
| 89 | assert norm_path(r'\\.\C:\Users') == r'\\.\C:\Users' |
| 90 | assert norm_path(r'\\') == '\\' |
| 91 | assert norm_path(r'//') == '\\' |
| 92 | assert norm_path(r'\\\') == '\\' |
| 93 | assert norm_path(r'.') == '.' |
| 94 | assert norm_path(r'\\Server') == '\\Server' |
| 95 | assert norm_path(r'\\Server\') == '\\Server' |
| 96 | return |
| 97 | } |
| 98 | assert norm_path('/path/././../to/file//file.v/.') == '/to/file/file.v' |
| 99 | assert norm_path('path/././to/files/../../file.v/.') == 'path/file.v' |
| 100 | assert norm_path('path/././/../../to/file.v/.') == '../to/file.v' |
| 101 | assert norm_path('/path/././/../..///.././file.v/././') == '/file.v' |
| 102 | assert norm_path('path/././//../../../to/dir//.././file.v/././') == '../../to/file.v' |
| 103 | assert norm_path('path/../dir/..') == '.' |
| 104 | assert norm_path('../dir/..') == '..' |
| 105 | assert norm_path('/../dir/..') == '/' |
| 106 | assert norm_path('//././dir/../files/././/file.v') == '/files/file.v' |
| 107 | assert norm_path('/\\../dir/////////.') == '/\\../dir' |
| 108 | assert norm_path('/home/') == '/home' |
| 109 | assert norm_path('/home/////./.') == '/home' |
| 110 | assert norm_path('...') == '...' |
| 111 | } |
| 112 | |
| 113 | fn test_abs_path() { |
| 114 | wd := getwd() |
| 115 | wd_w_sep := wd + path_separator |
| 116 | $if windows { |
| 117 | assert abs_path('path/to/file.v') == '${wd_w_sep}path\\to\\file.v' |
| 118 | assert abs_path('path/to/file.v') == '${wd_w_sep}path\\to\\file.v' |
| 119 | assert abs_path('/') == r'\' |
| 120 | assert abs_path(r'C:\path\to\files\file.v') == r'C:\path\to\files\file.v' |
| 121 | assert abs_path(r'C:\/\path\.\to\../files\file.v\.\\\.\') == r'C:\path\files\file.v' |
| 122 | assert abs_path(r'\\Host\share\files\..\..\.') == r'\\Host\share\' |
| 123 | assert abs_path(r'\\.\HardDiskvolume2\files\..\..\.') == r'\\.\HardDiskvolume2\' |
| 124 | assert abs_path(r'\\?\share') == r'\\?\share' |
| 125 | assert abs_path(r'\\.\') == r'\' |
| 126 | assert abs_path(r'G:/\..\\..\.\.\file.v\\.\.\\\\') == r'G:\file.v' |
| 127 | assert abs_path('files') == '${wd_w_sep}files' |
| 128 | assert abs_path('') == wd |
| 129 | assert abs_path('.') == wd |
| 130 | assert abs_path('files/../file.v') == '${wd_w_sep}file.v' |
| 131 | assert abs_path('///') == r'\' |
| 132 | assert abs_path('/path/to/file.v') == r'\path\to\file.v' |
| 133 | assert abs_path('D:/') == r'D:\' |
| 134 | assert abs_path(r'\\.\HardiskVolume6') == r'\\.\HardiskVolume6' |
| 135 | return |
| 136 | } |
| 137 | assert abs_path('/') == '/' |
| 138 | assert abs_path('.') == wd |
| 139 | assert abs_path('files') == '${wd_w_sep}files' |
| 140 | assert abs_path('') == wd |
| 141 | assert abs_path('files/../file.v') == '${wd_w_sep}file.v' |
| 142 | assert abs_path('///') == '/' |
| 143 | assert abs_path('/path/to/file.v') == '/path/to/file.v' |
| 144 | assert abs_path('/path/to/file.v/../..') == '/path' |
| 145 | assert abs_path('path/../file.v/..') == wd |
| 146 | assert abs_path('///') == '/' |
| 147 | } |
| 148 | |
| 149 | fn test_existing_path() { |
| 150 | wd := getwd() |
| 151 | $if windows { |
| 152 | assert existing_path('') or { '' } == '' |
| 153 | assert existing_path('..') or { '' } == '..' |
| 154 | assert existing_path('.') or { '' } == '.' |
| 155 | assert existing_path(wd) or { '' } == wd |
| 156 | assert existing_path('\\') or { '' } == '\\' |
| 157 | assert existing_path('${wd}\\.\\\\does/not/exist\\.\\') or { '' } == '${wd}\\.\\\\' |
| 158 | assert existing_path('${wd}\\\\/\\.\\.\\/.') or { '' } == '${wd}\\\\/\\.\\.\\/.' |
| 159 | assert existing_path('${wd}\\././/\\/oh') or { '' } == '${wd}\\././/\\/' |
| 160 | return |
| 161 | } |
| 162 | assert existing_path('') or { '' } == '' |
| 163 | assert existing_path('..') or { '' } == '..' |
| 164 | assert existing_path('.') or { '' } == '.' |
| 165 | assert existing_path(wd) or { '' } == wd |
| 166 | assert existing_path('/') or { '' } == '/' |
| 167 | assert existing_path('${wd}/does/.///not/exist///.//') or { '' } == '${wd}/' |
| 168 | assert existing_path('${wd}//././/.//') or { '' } == '${wd}//././/.//' |
| 169 | assert existing_path('${wd}//././/.//oh') or { '' } == '${wd}//././/.//' |
| 170 | } |
| 171 | |
| 172 | fn test_windows_volume() { |
| 173 | $if windows { |
| 174 | assert windows_volume('C:/path\\to/file.v') == 'C:' |
| 175 | assert windows_volume('D:\\.\\') == 'D:' |
| 176 | assert windows_volume('G:') == 'G:' |
| 177 | assert windows_volume('G') == '' |
| 178 | assert windows_volume(r'\\Host\share\files\file.v') == r'\\Host\share' |
| 179 | assert windows_volume('\\\\Host\\') == '' |
| 180 | assert windows_volume(r'\\.\BootPartition2\\files\.\\') == r'\\.\BootPartition2' |
| 181 | assert windows_volume(r'\/.\BootPartition2\\files\.\\') == r'\/.\BootPartition2' |
| 182 | assert windows_volume(r'\\\.\BootPartition2\\files\.\\') == '' |
| 183 | assert windows_volume('') == '' |
| 184 | assert windows_volume('\\') == '' |
| 185 | assert windows_volume('/') == '' |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | fn test_trim_extended_length_path_prefix() { |
| 190 | $if windows { |
| 191 | assert trim_extended_length_path_prefix(r'\\?\C:\path\to\file.v') == r'C:\path\to\file.v' |
| 192 | assert trim_extended_length_path_prefix(r'\\?\UNC\host\share\file.v') == r'\\host\share\file.v' |
| 193 | assert trim_extended_length_path_prefix(r'\\?\Volume{01234567-89ab-cdef-0123-456789abcdef}\file.v') == r'\\?\Volume{01234567-89ab-cdef-0123-456789abcdef}\file.v' |
| 194 | assert trim_extended_length_path_prefix(r'C:\path\to\file.v') == r'C:\path\to\file.v' |
| 195 | assert trim_extended_length_path_prefix('') == '' |
| 196 | } |
| 197 | } |
| 198 | |