| 1 | import os |
| 2 | import os.font |
| 3 | |
| 4 | fn env_snapshot(name string) (string, bool) { |
| 5 | val := os.getenv_opt(name) or { return '', false } |
| 6 | return val, true |
| 7 | } |
| 8 | |
| 9 | fn restore_env(name string, value string, existed bool) { |
| 10 | if existed { |
| 11 | os.setenv(name, value, true) |
| 12 | } else { |
| 13 | os.unsetenv(name) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | fn normalize_test_path(path string) string { |
| 18 | return path.replace(os.path_separator, '/') |
| 19 | } |
| 20 | |
| 21 | fn test_tmpdir() { |
| 22 | t := os.temp_dir() |
| 23 | assert t.len > 0 |
| 24 | assert os.is_dir(t) |
| 25 | tfile := t + os.path_separator + 'tmpfile.txt' |
| 26 | os.rm(tfile) or {} // just in case |
| 27 | tfile_content := 'this is a temporary file' |
| 28 | os.write_file(tfile, tfile_content) or { panic(err) } |
| 29 | tfile_content_read := os.read_file(tfile) or { panic(err) } |
| 30 | assert tfile_content_read == tfile_content |
| 31 | os.rm(tfile) or { panic(err) } |
| 32 | } |
| 33 | |
| 34 | fn test_ensure_folder_is_writable() { |
| 35 | tmp := os.temp_dir() |
| 36 | os.ensure_folder_is_writable(tmp) or { |
| 37 | eprintln('err: ${err}') |
| 38 | assert false |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn test_expand_tilde_to_home() { |
| 43 | os.setenv('HOME', '/tmp/home/folder', true) |
| 44 | os.setenv('USERPROFILE', r'\tmp\home\folder', true) |
| 45 | |
| 46 | home_test := os.join_path(os.home_dir(), 'test', 'tilde', 'expansion') |
| 47 | home_expansion_test := os.expand_tilde_to_home(os.join_path('~', 'test', 'tilde', 'expansion')) |
| 48 | assert home_test == home_expansion_test |
| 49 | assert os.expand_tilde_to_home('~') == os.home_dir() |
| 50 | } |
| 51 | |
| 52 | fn test_config_dir() { |
| 53 | cdir := os.config_dir()! |
| 54 | assert cdir.len > 0 |
| 55 | adir := '${cdir}/test-v-config' |
| 56 | os.mkdir_all(adir)! |
| 57 | os.rmdir(adir)! |
| 58 | assert os.is_dir(cdir) |
| 59 | } |
| 60 | |
| 61 | fn test_data_dir_prefers_platform_location() { |
| 62 | xdg_data_home, had_xdg_data_home := env_snapshot('XDG_DATA_HOME') |
| 63 | defer { |
| 64 | restore_env('XDG_DATA_HOME', xdg_data_home, had_xdg_data_home) |
| 65 | } |
| 66 | $if windows { |
| 67 | local_app_data, had_local_app_data := env_snapshot('LocalAppData') |
| 68 | userprofile, had_userprofile := env_snapshot('USERPROFILE') |
| 69 | test_root := os.join_path(os.temp_dir(), 'v_data_dir_windows_test_${os.getpid()}') |
| 70 | defer { |
| 71 | restore_env('LocalAppData', local_app_data, had_local_app_data) |
| 72 | restore_env('USERPROFILE', userprofile, had_userprofile) |
| 73 | os.rmdir_all(test_root) or {} |
| 74 | } |
| 75 | expected := os.join_path(test_root, 'LocalAppData') |
| 76 | os.setenv('XDG_DATA_HOME', os.join_path(test_root, 'XdgDataHome'), true) |
| 77 | os.setenv('LocalAppData', expected, true) |
| 78 | os.setenv('USERPROFILE', os.join_path(test_root, 'UserProfile'), true) |
| 79 | assert os.data_dir() == expected |
| 80 | assert os.is_dir(expected) |
| 81 | } $else { |
| 82 | test_root := os.join_path(os.temp_dir(), 'v_data_dir_xdg_test_${os.getpid()}') |
| 83 | defer { |
| 84 | os.rmdir_all(test_root) or {} |
| 85 | } |
| 86 | expected := os.join_path(test_root, 'XdgDataHome') |
| 87 | os.setenv('XDG_DATA_HOME', expected, true) |
| 88 | assert os.data_dir() == expected |
| 89 | assert os.is_dir(expected) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | fn test_vmodules_dir_without_home_falls_back_to_vtmp() { |
| 94 | home, had_home := env_snapshot('HOME') |
| 95 | userprofile, had_userprofile := env_snapshot('USERPROFILE') |
| 96 | vmodules, had_vmodules := env_snapshot('VMODULES') |
| 97 | defer { |
| 98 | restore_env('HOME', home, had_home) |
| 99 | restore_env('USERPROFILE', userprofile, had_userprofile) |
| 100 | restore_env('VMODULES', vmodules, had_vmodules) |
| 101 | } |
| 102 | os.unsetenv('HOME') |
| 103 | os.unsetenv('USERPROFILE') |
| 104 | os.unsetenv('VMODULES') |
| 105 | assert os.vmodules_dir() == os.join_path_single(os.vtmp_dir(), '.vmodules') |
| 106 | } |
| 107 | |
| 108 | fn test_font_default_prefers_user_config_dir_font() ! { |
| 109 | vui_font, had_vui_font := env_snapshot('VUI_FONT') |
| 110 | defer { |
| 111 | restore_env('VUI_FONT', vui_font, had_vui_font) |
| 112 | } |
| 113 | os.unsetenv('VUI_FONT') |
| 114 | $if windows { |
| 115 | app_data, had_app_data := env_snapshot('AppData') |
| 116 | test_root := os.join_path(os.temp_dir(), 'v_font_config_windows_test_${os.getpid()}') |
| 117 | defer { |
| 118 | restore_env('AppData', app_data, had_app_data) |
| 119 | os.rmdir_all(test_root) or {} |
| 120 | } |
| 121 | os.setenv('AppData', test_root, true) |
| 122 | font_dir := os.join_path(test_root, 'v', 'fonts') |
| 123 | expected := os.join_path(font_dir, 'UserFallback.ttf') |
| 124 | os.mkdir_all(font_dir)! |
| 125 | os.write_file(expected, 'fallback')! |
| 126 | assert normalize_test_path(font.default()) == normalize_test_path(expected) |
| 127 | } $else $if macos || darwin || ios { |
| 128 | home, had_home := env_snapshot('HOME') |
| 129 | userprofile, had_userprofile := env_snapshot('USERPROFILE') |
| 130 | test_root := os.join_path(os.temp_dir(), 'v_font_config_macos_test_${os.getpid()}') |
| 131 | defer { |
| 132 | restore_env('HOME', home, had_home) |
| 133 | restore_env('USERPROFILE', userprofile, had_userprofile) |
| 134 | os.rmdir_all(test_root) or {} |
| 135 | } |
| 136 | os.setenv('HOME', test_root, true) |
| 137 | os.unsetenv('USERPROFILE') |
| 138 | font_dir := os.join_path(test_root, 'Library', 'Application Support', 'v', 'fonts') |
| 139 | expected := os.join_path(font_dir, 'UserFallback.ttf') |
| 140 | os.mkdir_all(font_dir)! |
| 141 | os.write_file(expected, 'fallback')! |
| 142 | assert normalize_test_path(font.default()) == normalize_test_path(expected) |
| 143 | } $else { |
| 144 | xdg_config_home, had_xdg_config_home := env_snapshot('XDG_CONFIG_HOME') |
| 145 | home, had_home := env_snapshot('HOME') |
| 146 | test_root := os.join_path(os.temp_dir(), 'v_font_config_xdg_test_${os.getpid()}') |
| 147 | defer { |
| 148 | restore_env('XDG_CONFIG_HOME', xdg_config_home, had_xdg_config_home) |
| 149 | restore_env('HOME', home, had_home) |
| 150 | os.rmdir_all(test_root) or {} |
| 151 | } |
| 152 | os.setenv('XDG_CONFIG_HOME', test_root, true) |
| 153 | os.setenv('HOME', test_root, true) |
| 154 | font_dir := os.join_path(test_root, 'v', 'fonts') |
| 155 | expected := os.join_path(font_dir, 'UserFallback.ttf') |
| 156 | os.mkdir_all(font_dir)! |
| 157 | os.write_file(expected, 'fallback')! |
| 158 | assert normalize_test_path(font.default()) == normalize_test_path(expected) |
| 159 | } |
| 160 | } |
| 161 | |