|
| 1 | +require 'test/unit' |
| 2 | + |
| 3 | +GIT_URLS = [ |
| 4 | + { |
| 5 | + url: 'ssh://host.xz/path/to/repo.git/', |
| 6 | + expected_attributes: { scheme: 'ssh', host: 'host.xz', path: '/path/to/repo.git/' }, |
| 7 | + expected_clone_to: 'repo' |
| 8 | + }, |
| 9 | + { |
| 10 | + url: 'ssh://host.xz:4443/path/to/repo.git/', |
| 11 | + expected_attributes: { scheme: 'ssh', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' }, |
| 12 | + expected_clone_to: 'repo' |
| 13 | + }, |
| 14 | + { |
| 15 | + url: 'ssh:///path/to/repo.git/', |
| 16 | + expected_attributes: { scheme: 'ssh', host: '', path: '/path/to/repo.git/' }, |
| 17 | + expected_clone_to: 'repo' |
| 18 | + }, |
| 19 | + { |
| 20 | + url: 'user@host.xz:path/to/repo.git/', |
| 21 | + expected_attributes: { scheme: 'git-alt', user: 'user', host: 'host.xz', path: '/path/to/repo.git/' }, |
| 22 | + expected_clone_to: 'repo' |
| 23 | + }, |
| 24 | + { |
| 25 | + url: 'host.xz:path/to/repo.git/', |
| 26 | + expected_attributes: { scheme: 'git-alt', host: 'host.xz', path: '/path/to/repo.git/' }, |
| 27 | + expected_clone_to: 'repo' |
| 28 | + }, |
| 29 | + { |
| 30 | + url: 'git://host.xz:4443/path/to/repo.git/', |
| 31 | + expected_attributes: { scheme: 'git', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' }, |
| 32 | + expected_clone_to: 'repo' |
| 33 | + }, |
| 34 | + { |
| 35 | + url: 'git://user@host.xz:4443/path/to/repo.git/', |
| 36 | + expected_attributes: { scheme: 'git', user: 'user', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' }, |
| 37 | + expected_clone_to: 'repo' |
| 38 | + }, |
| 39 | + { |
| 40 | + url: 'https://host.xz/path/to/repo.git/', |
| 41 | + expected_attributes: { scheme: 'https', host: 'host.xz', path: '/path/to/repo.git/' }, |
| 42 | + expected_clone_to: 'repo' |
| 43 | + }, |
| 44 | + { |
| 45 | + url: 'https://host.xz:4443/path/to/repo.git/', |
| 46 | + expected_attributes: { scheme: 'https', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' }, |
| 47 | + expected_clone_to: 'repo' |
| 48 | + }, |
| 49 | + { |
| 50 | + url: 'ftps://host.xz:4443/path/to/repo.git/', |
| 51 | + expected_attributes: { scheme: 'ftps', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' }, |
| 52 | + expected_clone_to: 'repo' |
| 53 | + }, |
| 54 | + { |
| 55 | + url: 'ftps://host.xz:4443/path/to/repo.git/', |
| 56 | + expected_attributes: { scheme: 'ftps', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' }, |
| 57 | + expected_clone_to: 'repo' |
| 58 | + }, |
| 59 | + { |
| 60 | + url: 'file:./relative-path/to/repo.git/', |
| 61 | + expected_attributes: { scheme: 'file', path: './relative-path/to/repo.git/' }, |
| 62 | + expected_clone_to: 'repo' |
| 63 | + }, |
| 64 | + { |
| 65 | + url: 'file:///path/to/repo.git/', |
| 66 | + expected_attributes: { scheme: 'file', host: '', path: '/path/to/repo.git/' }, |
| 67 | + expected_clone_to: 'repo' |
| 68 | + }, |
| 69 | + { |
| 70 | + url: 'file:///path/to/repo.git', |
| 71 | + expected_attributes: { scheme: 'file', host: '', path: '/path/to/repo.git' }, |
| 72 | + expected_clone_to: 'repo' |
| 73 | + }, |
| 74 | + { |
| 75 | + url: 'file://host.xz/path/to/repo.git', |
| 76 | + expected_attributes: { scheme: 'file', host: 'host.xz', path: '/path/to/repo.git' }, |
| 77 | + expected_clone_to: 'repo' |
| 78 | + }, |
| 79 | + { |
| 80 | + url: '/path/to/repo.git/', |
| 81 | + expected_attributes: { path: '/path/to/repo.git/' }, |
| 82 | + expected_clone_to: 'repo' |
| 83 | + }, |
| 84 | + { |
| 85 | + url: '/path/to/bare-repo/.git', |
| 86 | + expected_attributes: { path: '/path/to/bare-repo/.git' }, |
| 87 | + expected_clone_to: 'bare-repo' |
| 88 | + }, |
| 89 | + { |
| 90 | + url: 'relative-path/to/repo.git/', |
| 91 | + expected_attributes: { path: 'relative-path/to/repo.git/' }, |
| 92 | + expected_clone_to: 'repo' |
| 93 | + }, |
| 94 | + { |
| 95 | + url: './relative-path/to/repo.git/', |
| 96 | + expected_attributes: { path: './relative-path/to/repo.git/' }, |
| 97 | + expected_clone_to: 'repo' |
| 98 | + }, |
| 99 | + { |
| 100 | + url: '../ruby-git/.git', |
| 101 | + expected_attributes: { path: '../ruby-git/.git' }, |
| 102 | + expected_clone_to: 'ruby-git' |
| 103 | + } |
| 104 | +].freeze |
| 105 | + |
| 106 | +# Tests for the Git::URL class |
| 107 | +# |
| 108 | +class TestURL < Test::Unit::TestCase |
| 109 | + def test_parse |
| 110 | + GIT_URLS.each do |url_data| |
| 111 | + url = url_data[:url] |
| 112 | + expected_attributes = url_data[:expected_attributes] |
| 113 | + actual_attributes = Git::URL.parse(url).to_hash.delete_if {| key, value | value.nil? } |
| 114 | + assert_equal(expected_attributes, actual_attributes, "Failed to parse URL '#{url}' correctly") |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + def test_clone_to |
| 119 | + GIT_URLS.each do |url_data| |
| 120 | + url = url_data[:url] |
| 121 | + expected_clone_to = url_data[:expected_clone_to] |
| 122 | + actual_repo_name = Git::URL.clone_to(url) |
| 123 | + assert_equal( |
| 124 | + expected_clone_to, actual_repo_name, |
| 125 | + "Failed to determine the repository directory for URL '#{url}' correctly" |
| 126 | + ) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + def test_to_s |
| 131 | + GIT_URLS.each do |url_data| |
| 132 | + url = url_data[:url] |
| 133 | + to_s = Git::URL.parse(url).to_s |
| 134 | + assert_equal(url, to_s, "Parsed URI#to_s does not return the original URL '#{url}' correctly") |
| 135 | + end |
| 136 | + end |
| 137 | +end |
0 commit comments