|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test/unit' |
| 4 | + |
| 5 | +# Tests Git::URL.parse |
| 6 | +# |
| 7 | +class TestURLParse < Test::Unit::TestCase |
| 8 | + def test_parse_with_invalid_url |
| 9 | + url = 'user@host.xz:/path/to/repo.git/' |
| 10 | + assert_raise(Addressable::URI::InvalidURIError) do |
| 11 | + Git::URL.parse(url) |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + def test_parse |
| 16 | + GIT_URLS.each do |url_data| |
| 17 | + url = url_data[:url] |
| 18 | + expected_uri = url_data[:expected_uri] |
| 19 | + actual_uri = Git::URL.parse(url).to_hash.delete_if { |_key, value| value.nil? } |
| 20 | + assert_equal(expected_uri, actual_uri, "Failed to parse URL '#{url}' correctly") |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + # For any URL, #to_s should return the url passed to Git::URL.parse(url) |
| 25 | + def test_to_s |
| 26 | + GIT_URLS.each do |url_data| |
| 27 | + url = url_data[:url] |
| 28 | + to_s = Git::URL.parse(url).to_s |
| 29 | + assert_equal(url, to_s, "Parsed URI#to_s does not return the original URL '#{url}' correctly") |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + GIT_URLS = [ |
| 34 | + { |
| 35 | + url: 'ssh://host.xz/path/to/repo.git/', |
| 36 | + expected_uri: { scheme: 'ssh', host: 'host.xz', path: '/path/to/repo.git/' } |
| 37 | + }, |
| 38 | + { |
| 39 | + url: 'ssh://host.xz:4443/path/to/repo.git/', |
| 40 | + expected_uri: { scheme: 'ssh', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' } |
| 41 | + }, |
| 42 | + { |
| 43 | + url: 'ssh:///path/to/repo.git/', |
| 44 | + expected_uri: { scheme: 'ssh', host: '', path: '/path/to/repo.git/' } |
| 45 | + }, |
| 46 | + { |
| 47 | + url: 'user@host.xz:path/to/repo.git/', |
| 48 | + expected_uri: { scheme: 'git-alt', user: 'user', host: 'host.xz', path: '/path/to/repo.git/' } |
| 49 | + }, |
| 50 | + { |
| 51 | + url: 'host.xz:path/to/repo.git/', |
| 52 | + expected_uri: { scheme: 'git-alt', host: 'host.xz', path: '/path/to/repo.git/' } |
| 53 | + }, |
| 54 | + { |
| 55 | + url: 'git://host.xz:4443/path/to/repo.git/', |
| 56 | + expected_uri: { scheme: 'git', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' } |
| 57 | + }, |
| 58 | + { |
| 59 | + url: 'git://user@host.xz:4443/path/to/repo.git/', |
| 60 | + expected_uri: { scheme: 'git', user: 'user', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' } |
| 61 | + }, |
| 62 | + { |
| 63 | + url: 'https://host.xz/path/to/repo.git/', |
| 64 | + expected_uri: { scheme: 'https', host: 'host.xz', path: '/path/to/repo.git/' } |
| 65 | + }, |
| 66 | + { |
| 67 | + url: 'https://host.xz:4443/path/to/repo.git/', |
| 68 | + expected_uri: { scheme: 'https', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' } |
| 69 | + }, |
| 70 | + { |
| 71 | + url: 'ftps://host.xz:4443/path/to/repo.git/', |
| 72 | + expected_uri: { scheme: 'ftps', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' } |
| 73 | + }, |
| 74 | + { |
| 75 | + url: 'ftps://host.xz:4443/path/to/repo.git/', |
| 76 | + expected_uri: { scheme: 'ftps', host: 'host.xz', port: 4443, path: '/path/to/repo.git/' } |
| 77 | + }, |
| 78 | + { |
| 79 | + url: 'file:./relative-path/to/repo.git/', |
| 80 | + expected_uri: { scheme: 'file', path: './relative-path/to/repo.git/' } |
| 81 | + }, |
| 82 | + { |
| 83 | + url: 'file:///path/to/repo.git/', |
| 84 | + expected_uri: { scheme: 'file', host: '', path: '/path/to/repo.git/' } |
| 85 | + }, |
| 86 | + { |
| 87 | + url: 'file:///path/to/repo.git', |
| 88 | + expected_uri: { scheme: 'file', host: '', path: '/path/to/repo.git' } |
| 89 | + }, |
| 90 | + { |
| 91 | + url: 'file://host.xz/path/to/repo.git', |
| 92 | + expected_uri: { scheme: 'file', host: 'host.xz', path: '/path/to/repo.git' } |
| 93 | + }, |
| 94 | + { url: '/path/to/repo.git/', expected_uri: { path: '/path/to/repo.git/' } }, |
| 95 | + { url: '/path/to/bare-repo/.git', expected_uri: { path: '/path/to/bare-repo/.git' } }, |
| 96 | + { url: 'relative-path/to/repo.git/', expected_uri: { path: 'relative-path/to/repo.git/' } }, |
| 97 | + { url: './relative-path/to/repo.git/', expected_uri: { path: './relative-path/to/repo.git/' } }, |
| 98 | + { url: '../ruby-git/.git', expected_uri: { path: '../ruby-git/.git' } } |
| 99 | + ].freeze |
| 100 | +end |
0 commit comments