| 1 | module vshare |
| 2 | |
| 3 | import os |
| 4 | import time |
| 5 | |
| 6 | fn test_copy_to_clipboard_with_commands_returns_false_without_candidates() { |
| 7 | assert copy_to_clipboard_with_commands('https://play.vlang.io/p/test', []) == false |
| 8 | } |
| 9 | |
| 10 | fn test_copy_to_clipboard_with_commands_uses_the_first_working_command() { |
| 11 | test_dir := os.join_path(os.vtmp_dir(), 'vshare_test_${os.getpid()}_${time.now().unix_micro()}') |
| 12 | os.mkdir_all(test_dir)! |
| 13 | defer { |
| 14 | os.rmdir_all(test_dir) or {} |
| 15 | } |
| 16 | output_path := os.join_path(test_dir, 'clipboard.out') |
| 17 | fake_clip := os.join_path(test_dir, fake_clipboard_executable_name()) |
| 18 | os.write_file(fake_clip, fake_clipboard_script())! |
| 19 | $if !windows { |
| 20 | os.chmod(fake_clip, 0o700)! |
| 21 | } |
| 22 | original_path := os.getenv('PATH') |
| 23 | original_output_path := os.getenv('VSHARE_TEST_OUTPUT') |
| 24 | new_path := if original_path.len == 0 { |
| 25 | test_dir |
| 26 | } else { |
| 27 | '${test_dir}${os.path_delimiter}${original_path}' |
| 28 | } |
| 29 | os.setenv('PATH', new_path, true) |
| 30 | os.setenv('VSHARE_TEST_OUTPUT', output_path, true) |
| 31 | defer { |
| 32 | os.setenv('PATH', original_path, true) |
| 33 | if original_output_path.len == 0 { |
| 34 | os.unsetenv('VSHARE_TEST_OUTPUT') |
| 35 | } else { |
| 36 | os.setenv('VSHARE_TEST_OUTPUT', original_output_path, true) |
| 37 | } |
| 38 | } |
| 39 | assert copy_to_clipboard_with_commands('https://play.vlang.io/p/test', [ |
| 40 | ClipboardCommand{ |
| 41 | executable: 'missing-clipboard-command' |
| 42 | command: 'missing-clipboard-command < @FILE@' |
| 43 | }, |
| 44 | ClipboardCommand{ |
| 45 | executable: fake_clipboard_executable_name() |
| 46 | command: fake_clipboard_command() |
| 47 | }, |
| 48 | ]) |
| 49 | output := os.read_file(output_path)! |
| 50 | $if windows { |
| 51 | assert output.trim_right('\r\n') == 'https://play.vlang.io/p/test' |
| 52 | } $else { |
| 53 | assert output == 'https://play.vlang.io/p/test' |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn fake_clipboard_command() string { |
| 58 | return '${fake_clipboard_executable_name()} < @FILE@' |
| 59 | } |
| 60 | |
| 61 | fn fake_clipboard_executable_name() string { |
| 62 | $if windows { |
| 63 | return 'fakeclip.bat' |
| 64 | } $else { |
| 65 | return 'fakeclip' |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn fake_clipboard_script() string { |
| 70 | $if windows { |
| 71 | return '@echo off\r\nmore > "%VSHARE_TEST_OUTPUT%"\r\n' |
| 72 | } $else { |
| 73 | return '#!/bin/sh\ncat > "\$VSHARE_TEST_OUTPUT"\n' |
| 74 | } |
| 75 | } |
| 76 | |