|
| 1 | +pub mod util; |
| 2 | + |
| 3 | +mod with_overrides { |
| 4 | + use std::borrow::Cow; |
| 5 | + |
| 6 | + use gix_object::bstr::BStr; |
| 7 | + use gix_sec::Permission; |
| 8 | + use gix_testtools::Env; |
| 9 | + use serial_test::serial; |
| 10 | + |
| 11 | + use crate::util::named_subrepo_opts; |
| 12 | + |
| 13 | + #[test] |
| 14 | + #[serial] |
| 15 | + fn order_from_api_and_cli_and_environment() -> gix_testtools::Result { |
| 16 | + let default_date = "1979-02-26 18:30:00"; |
| 17 | + let _env = Env::new() |
| 18 | + .set("GIT_HTTP_USER_AGENT", "agent-from-env") |
| 19 | + .set("GIT_HTTP_LOW_SPEED_LIMIT", "1") |
| 20 | + .set("GIT_HTTP_LOW_SPEED_TIME", "1") |
| 21 | + .set("GIT_HTTP_PROXY_AUTHMETHOD", "proxy-auth-method-env") |
| 22 | + .set("GIT_CURL_VERBOSE", "true") |
| 23 | + .set("https_proxy", "https-lower-override") |
| 24 | + .set("HTTPS_PROXY", "https-upper") |
| 25 | + .set("http_proxy", "http-lower") |
| 26 | + .set("all_proxy", "all-proxy-lower") |
| 27 | + .set("ALL_PROXY", "all-proxy") |
| 28 | + .set("no_proxy", "no-proxy-lower") |
| 29 | + .set("NO_PROXY", "no-proxy") |
| 30 | + .set("GIT_PROTOCOL_FROM_USER", "file-allowed") |
| 31 | + .set("GIT_REPLACE_REF_BASE", "refs/replace-mine") |
| 32 | + .set("GIT_NO_REPLACE_OBJECTS", "no-replace") |
| 33 | + .set("GIT_COMMITTER_NAME", "committer name") |
| 34 | + .set("GIT_COMMITTER_EMAIL", "committer email") |
| 35 | + .set("GIT_COMMITTER_DATE", default_date) |
| 36 | + .set("GIT_AUTHOR_NAME", "author name") |
| 37 | + .set("GIT_AUTHOR_EMAIL", "author email") |
| 38 | + .set("GIT_AUTHOR_DATE", default_date) |
| 39 | + .set("EMAIL", "user email") |
| 40 | + .set("GITOXIDE_PACK_CACHE_MEMORY", "0") |
| 41 | + .set("GITOXIDE_OBJECT_CACHE_MEMORY", "5m") |
| 42 | + .set("GIT_SSL_CAINFO", "./env.pem") |
| 43 | + .set("GIT_SSL_VERSION", "tlsv1.3") |
| 44 | + .set("GIT_SSH_VARIANT", "ssh-variant-env") |
| 45 | + .set("GIT_SSH_COMMAND", "ssh-command-env") |
| 46 | + .set("GIT_SSH", "ssh-command-fallback-env") |
| 47 | + .set("GIT_LITERAL_PATHSPECS", "pathspecs-literal") |
| 48 | + .set("GIT_GLOB_PATHSPECS", "pathspecs-glob") |
| 49 | + .set("GIT_NOGLOB_PATHSPECS", "pathspecs-noglob") |
| 50 | + .set("GIT_ICASE_PATHSPECS", "pathspecs-icase") |
| 51 | + .set("GIT_SHALLOW_FILE", "shallow-file-env"); |
| 52 | + let mut opts = gix::open::Options::isolated() |
| 53 | + .cli_overrides([ |
| 54 | + "http.userAgent=agent-from-cli", |
| 55 | + "http.lowSpeedLimit=3", |
| 56 | + "http.lowSpeedTime=3", |
| 57 | + "http.sslCAInfo=./cli.pem", |
| 58 | + "http.sslVersion=sslv3", |
| 59 | + "ssh.variant=ssh-variant-cli", |
| 60 | + "core.sshCommand=ssh-command-cli", |
| 61 | + "gitoxide.ssh.commandWithoutShellFallback=ssh-command-fallback-cli", |
| 62 | + "gitoxide.http.proxyAuthMethod=proxy-auth-method-cli", |
| 63 | + "gitoxide.core.shallowFile=shallow-file-cli", |
| 64 | + ]) |
| 65 | + .config_overrides([ |
| 66 | + "http.userAgent=agent-from-api", |
| 67 | + "http.lowSpeedLimit=2", |
| 68 | + "http.lowSpeedTime=2", |
| 69 | + "http.sslCAInfo=./api.pem", |
| 70 | + "http.sslVersion=tlsv1", |
| 71 | + "ssh.variant=ssh-variant-api", |
| 72 | + "core.sshCommand=ssh-command-api", |
| 73 | + "gitoxide.ssh.commandWithoutShellFallback=ssh-command-fallback-api", |
| 74 | + "gitoxide.http.proxyAuthMethod=proxy-auth-method-api", |
| 75 | + "gitoxide.core.shallowFile=shallow-file-api", |
| 76 | + ]); |
| 77 | + opts.permissions.env.git_prefix = Permission::Allow; |
| 78 | + opts.permissions.env.http_transport = Permission::Allow; |
| 79 | + opts.permissions.env.identity = Permission::Allow; |
| 80 | + opts.permissions.env.objects = Permission::Allow; |
| 81 | + let repo = named_subrepo_opts("make_config_repos.sh", "http-config", opts)?; |
| 82 | + assert_eq!( |
| 83 | + repo.config_snapshot().meta().source, |
| 84 | + gix::config::Source::Local, |
| 85 | + "config always refers to the local one for safety" |
| 86 | + ); |
| 87 | + let config = repo.config_snapshot(); |
| 88 | + assert_eq!( |
| 89 | + config |
| 90 | + .strings_by_key("gitoxide.core.shallowFile") |
| 91 | + .expect("at least one value"), |
| 92 | + [ |
| 93 | + cow_bstr("shallow-file-cli"), |
| 94 | + cow_bstr("shallow-file-api"), |
| 95 | + cow_bstr("shallow-file-env") |
| 96 | + ] |
| 97 | + ); |
| 98 | + assert_eq!( |
| 99 | + config.strings_by_key("http.userAgent").expect("at least one value"), |
| 100 | + [ |
| 101 | + cow_bstr("agentJustForHttp"), |
| 102 | + cow_bstr("agent-from-cli"), |
| 103 | + cow_bstr("agent-from-api"), |
| 104 | + cow_bstr("agent-from-env") |
| 105 | + ] |
| 106 | + ); |
| 107 | + assert_eq!( |
| 108 | + config |
| 109 | + .integers_by_key("http.lowSpeedLimit") |
| 110 | + .transpose()? |
| 111 | + .expect("many values"), |
| 112 | + [5120, 3, 2, 1] |
| 113 | + ); |
| 114 | + assert_eq!( |
| 115 | + config |
| 116 | + .integers_by_key("http.lowSpeedTime") |
| 117 | + .transpose()? |
| 118 | + .expect("many values"), |
| 119 | + [10, 3, 2, 1] |
| 120 | + ); |
| 121 | + assert_eq!( |
| 122 | + config |
| 123 | + .strings_by_key("http.proxyAuthMethod") |
| 124 | + .expect("at least one value"), |
| 125 | + [cow_bstr("basic")], |
| 126 | + "this value isn't overridden directly" |
| 127 | + ); |
| 128 | + assert_eq!( |
| 129 | + config |
| 130 | + .strings_by_key("gitoxide.https.proxy") |
| 131 | + .expect("at least one value"), |
| 132 | + [ |
| 133 | + cow_bstr("https-upper"), |
| 134 | + cow_bstr(if cfg!(windows) { |
| 135 | + "https-upper" // on windows, environment variables are case-insensitive |
| 136 | + } else { |
| 137 | + "https-lower-override" |
| 138 | + }) |
| 139 | + ] |
| 140 | + ); |
| 141 | + assert_eq!( |
| 142 | + config |
| 143 | + .strings_by_key("gitoxide.http.proxy") |
| 144 | + .expect("at least one value"), |
| 145 | + [cow_bstr("http-lower")] |
| 146 | + ); |
| 147 | + assert_eq!( |
| 148 | + config |
| 149 | + .strings_by_key("gitoxide.http.allProxy") |
| 150 | + .expect("at least one value"), |
| 151 | + [ |
| 152 | + cow_bstr("all-proxy"), // on windows, environment variables are case-insensitive |
| 153 | + cow_bstr(if cfg!(windows) { "all-proxy" } else { "all-proxy-lower" }) |
| 154 | + ] |
| 155 | + ); |
| 156 | + assert_eq!( |
| 157 | + config |
| 158 | + .strings_by_key("gitoxide.http.noProxy") |
| 159 | + .expect("at least one value"), |
| 160 | + [ |
| 161 | + cow_bstr("no-proxy"), // on windows, environment variables are case-insensitive |
| 162 | + cow_bstr(if cfg!(windows) { "no-proxy" } else { "no-proxy-lower" }) |
| 163 | + ] |
| 164 | + ); |
| 165 | + assert_eq!( |
| 166 | + config.strings_by_key("http.sslCAInfo").expect("at least one value"), |
| 167 | + [ |
| 168 | + cow_bstr("./CA.pem"), |
| 169 | + cow_bstr("./cli.pem"), |
| 170 | + cow_bstr("./api.pem"), |
| 171 | + cow_bstr("./env.pem") |
| 172 | + ] |
| 173 | + ); |
| 174 | + assert_eq!( |
| 175 | + config.strings_by_key("http.sslVersion").expect("at least one value"), |
| 176 | + [ |
| 177 | + cow_bstr("sslv2"), |
| 178 | + cow_bstr("sslv3"), |
| 179 | + cow_bstr("tlsv1"), |
| 180 | + cow_bstr("tlsv1.3") |
| 181 | + ] |
| 182 | + ); |
| 183 | + assert_eq!( |
| 184 | + config.strings_by_key("ssh.variant").expect("at least one value"), |
| 185 | + [ |
| 186 | + cow_bstr("ssh-variant-cli"), |
| 187 | + cow_bstr("ssh-variant-api"), |
| 188 | + cow_bstr("ssh-variant-env"), |
| 189 | + ] |
| 190 | + ); |
| 191 | + assert_eq!( |
| 192 | + config.strings_by_key("core.sshCommand").expect("at least one value"), |
| 193 | + [ |
| 194 | + cow_bstr("ssh-command-cli"), |
| 195 | + cow_bstr("ssh-command-api"), |
| 196 | + cow_bstr("ssh-command-env"), |
| 197 | + ] |
| 198 | + ); |
| 199 | + assert_eq!( |
| 200 | + config |
| 201 | + .strings_by_key("gitoxide.ssh.commandWithoutShellFallback") |
| 202 | + .expect("at least one value"), |
| 203 | + [ |
| 204 | + cow_bstr("ssh-command-fallback-cli"), |
| 205 | + cow_bstr("ssh-command-fallback-api"), |
| 206 | + cow_bstr("ssh-command-fallback-env"), |
| 207 | + ] |
| 208 | + ); |
| 209 | + assert_eq!( |
| 210 | + config |
| 211 | + .strings_by_key("gitoxide.http.proxyAuthMethod") |
| 212 | + .expect("at least one value"), |
| 213 | + [ |
| 214 | + cow_bstr("proxy-auth-method-cli"), |
| 215 | + cow_bstr("proxy-auth-method-api"), |
| 216 | + cow_bstr("proxy-auth-method-env"), |
| 217 | + ] |
| 218 | + ); |
| 219 | + for (key, expected) in [ |
| 220 | + ("gitoxide.http.verbose", "true"), |
| 221 | + ("gitoxide.allow.protocolFromUser", "file-allowed"), |
| 222 | + ("core.useReplaceRefs", "no-replace"), |
| 223 | + ("gitoxide.objects.replaceRefBase", "refs/replace-mine"), |
| 224 | + ("gitoxide.committer.nameFallback", "committer name"), |
| 225 | + ("gitoxide.committer.emailFallback", "committer email"), |
| 226 | + ("gitoxide.author.nameFallback", "author name"), |
| 227 | + ("gitoxide.author.emailFallback", "author email"), |
| 228 | + ("gitoxide.commit.authorDate", default_date), |
| 229 | + ("gitoxide.commit.committerDate", default_date), |
| 230 | + ("gitoxide.user.emailFallback", "user email"), |
| 231 | + ("core.deltaBaseCacheLimit", "0"), |
| 232 | + ("gitoxide.objects.cacheLimit", "5m"), |
| 233 | + ("gitoxide.pathspec.icase", "pathspecs-icase"), |
| 234 | + ("gitoxide.pathspec.glob", "pathspecs-glob"), |
| 235 | + ("gitoxide.pathspec.noglob", "pathspecs-noglob"), |
| 236 | + ("gitoxide.pathspec.literal", "pathspecs-literal"), |
| 237 | + ] { |
| 238 | + assert_eq!( |
| 239 | + config |
| 240 | + .string_by_key(key) |
| 241 | + .unwrap_or_else(|| panic!("no value for {key}")) |
| 242 | + .as_ref(), |
| 243 | + expected, |
| 244 | + "{key} == {expected}" |
| 245 | + ); |
| 246 | + } |
| 247 | + Ok(()) |
| 248 | + } |
| 249 | + |
| 250 | + fn cow_bstr(s: &str) -> Cow<BStr> { |
| 251 | + Cow::Borrowed(s.into()) |
| 252 | + } |
| 253 | +} |
0 commit comments