aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/config_test.rs
blob: 0b88c6b599be0ed67b165c073ac92b7daab0adff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! ref: composer/tests/Composer/Test/ConfigTest.php

use indexmap::IndexMap;
use shirabe::config::Config;
use shirabe_php_shim::PhpMixed;

fn repo(r#type: &str, url: &str) -> PhpMixed {
    let mut m: IndexMap<String, PhpMixed> = IndexMap::new();
    m.insert("type".to_string(), PhpMixed::String(r#type.to_string()));
    m.insert("url".to_string(), PhpMixed::String(url.to_string()));
    PhpMixed::Array(m)
}

fn map(pairs: Vec<(&str, PhpMixed)>) -> IndexMap<String, PhpMixed> {
    pairs
        .into_iter()
        .map(|(k, v)| (k.to_string(), v))
        .collect()
}

fn disable(name: &str) -> PhpMixed {
    PhpMixed::Array(map(vec![(name, PhpMixed::Bool(false))]))
}

fn packagist() -> PhpMixed {
    repo("composer", "https://repo.packagist.org")
}

struct Case {
    expected: IndexMap<String, PhpMixed>,
    local: IndexMap<String, PhpMixed>,
    system: Option<IndexMap<String, PhpMixed>>,
}

/// ref: ConfigTest::dataAddPackagistRepository
fn data_add_packagist_repository() -> Vec<Case> {
    vec![
        // local config inherits system defaults
        Case {
            expected: map(vec![("packagist.org", packagist())]),
            local: map(vec![]),
            system: None,
        },
        // local config can disable system config by name
        Case {
            expected: map(vec![]),
            local: map(vec![("0", disable("packagist.org"))]),
            system: None,
        },
        // local config can disable system config by name bc
        Case {
            expected: map(vec![]),
            local: map(vec![("0", disable("packagist"))]),
            system: None,
        },
        // local config adds above defaults
        Case {
            expected: map(vec![
                ("0", repo("vcs", "git://github.com/composer/composer.git")),
                ("1", repo("pear", "http://pear.composer.org")),
                ("packagist.org", packagist()),
            ]),
            local: map(vec![
                ("0", repo("vcs", "git://github.com/composer/composer.git")),
                ("1", repo("pear", "http://pear.composer.org")),
            ]),
            system: None,
        },
        // system config adds above core defaults
        Case {
            expected: map(vec![
                ("example.com", repo("composer", "http://example.com")),
                ("packagist.org", packagist()),
            ]),
            local: map(vec![]),
            system: Some(map(vec![("example.com", repo("composer", "http://example.com"))])),
        },
        // local config can disable repos by name and re-add them anonymously to bring them above system config
        Case {
            expected: map(vec![
                ("1", repo("composer", "http://packagist.org")),
                ("example.com", repo("composer", "http://example.com")),
            ]),
            local: map(vec![
                ("0", disable("packagist.org")),
                ("1", repo("composer", "http://packagist.org")),
            ]),
            system: Some(map(vec![("example.com", repo("composer", "http://example.com"))])),
        },
        // local config can override by name to bring a repo above system config
        Case {
            expected: map(vec![
                ("packagist.org", repo("composer", "http://packagistnew.org")),
                ("example.com", repo("composer", "http://example.com")),
            ]),
            local: map(vec![("packagist.org", repo("composer", "http://packagistnew.org"))]),
            system: Some(map(vec![("example.com", repo("composer", "http://example.com"))])),
        },
        // local config redefining packagist.org by URL override it if no named keys are used
        Case {
            expected: map(vec![("0", repo("composer", "https://repo.packagist.org"))]),
            local: map(vec![("0", repo("composer", "https://repo.packagist.org"))]),
            system: None,
        },
        // local config redefining packagist.org by URL override it also with named keys
        Case {
            expected: map(vec![("example", repo("composer", "https://repo.packagist.org"))]),
            local: map(vec![("example", repo("composer", "https://repo.packagist.org"))]),
            system: None,
        },
        // incorrect local config does not cause ErrorException
        Case {
            expected: map(vec![
                ("packagist.org", packagist()),
                ("type", PhpMixed::String("vcs".to_string())),
                ("url", PhpMixed::String("http://example.com".to_string())),
            ]),
            local: map(vec![
                ("type", PhpMixed::String("vcs".to_string())),
                ("url", PhpMixed::String("http://example.com".to_string())),
            ]),
            system: None,
        },
    ]
}

#[test]
#[ignore = "Config::merge of an anonymous {name: false} disable entry reaches current() (todo!()) in the php-shim"]
fn test_add_packagist_repository() {
    for case in data_add_packagist_repository() {
        let mut config = Config::new(false, None);
        if let Some(system) = case.system {
            let mut cfg: IndexMap<String, PhpMixed> = IndexMap::new();
            cfg.insert("repositories".to_string(), PhpMixed::Array(system));
            config.merge(&cfg, "test");
        }
        let mut cfg: IndexMap<String, PhpMixed> = IndexMap::new();
        cfg.insert("repositories".to_string(), PhpMixed::Array(case.local));
        config.merge(&cfg, "test");

        let actual = config.get_repositories();

        // PHP assertEquals on arrays compares pairs irrespective of order.
        assert_eq!(case.expected.len(), actual.len());
        for (key, value) in &case.expected {
            assert_eq!(Some(value), actual.get(key), "repository key {key:?}");
        }
    }
}

// The remaining ConfigTest cases either read process env via Platform (process-timeout,
// htaccess-protect, var/realpath replacement, oauth, audit, ...) without the env isolation
// their setUp/tearDown provides, or exercise plugin-config merge details. They are not
// ported yet.
macro_rules! stub {
    ($name:ident) => {
        #[test]
        #[ignore = "not yet ported (env-dependent without the setUp/tearDown isolation, or plugin-config merge details)"]
        fn $name() {
            todo!()
        }
    };
}

stub!(test_preferred_install_as_string);
stub!(test_merge_preferred_install);
stub!(test_merge_github_oauth);
stub!(test_var_replacement);
stub!(test_realpath_replacement);
stub!(test_stream_wrapper_dirs);
stub!(test_fetching_relative_paths);
stub!(test_override_github_protocols);
stub!(test_git_disabled_by_default_in_github_protocols);
stub!(test_allowed_urls_pass);
stub!(test_prohibited_urls_throw_exception);
stub!(test_prohibited_urls_warning_verify_peer);
stub!(test_disable_tls_can_be_overridden);
stub!(test_process_timeout);
stub!(test_htaccess_protect);
stub!(test_get_source_of_value);
stub!(test_get_source_of_value_env_variables);
stub!(test_audit);
stub!(test_get_defaults_to_an_empty_array);
stub!(test_merges_plugin_config);
stub!(test_overrides_global_boolean_plugins_config);
stub!(test_allows_all_plugins_from_local_boolean);