Skip to content

Commit

Permalink
Set up machinery for testing HTTP interactions of HTTP registry (#818)
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Kaput <[email protected]>

---

**Stack**:
- #892
- #846
- #845
- #844
- #819
- #818⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do
not merge manually using the UI - doing so may have unexpected results.*
  • Loading branch information
mkaput authored Nov 13, 2023
1 parent a747a60 commit ecebc03
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 30 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ deno_task_shell = ">=0.13"
derive_builder = ">=0.12"
directories = "5"
dunce = "1"
expect-test = "1.4"
fs4 = { version = "0.7", features = ["tokio"] }
fs_extra = "1"
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
Expand Down
1 change: 1 addition & 0 deletions scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ reqwest = { workspace = true, features = ["rustls-tls-native-roots"] }

[dev-dependencies]
assert_fs.workspace = true
expect-test.workspace = true
fs_extra.workspace = true
io_tee.workspace = true
libc.workspace = true
Expand Down
95 changes: 92 additions & 3 deletions scarb/tests/http_registry.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fs;
use std::time::Duration;

use assert_fs::prelude::*;
use assert_fs::TempDir;
use expect_test::expect;
use indoc::indoc;
use std::fs;
use std::time::Duration;

use scarb_test_support::command::Scarb;
use scarb_test_support::project_builder::{Dep, DepBuilder, ProjectBuilder};
Expand Down Expand Up @@ -37,6 +39,52 @@ fn usage() {
.stdout_matches(indoc! {r#"
[..] Downloading bar v1.0.0 ([..])
"#});

let expected = expect![["
GET /config.json
accept: */*
accept-encoding: gzip, br, deflate
host: ...
user-agent: ...
200 OK
accept-ranges: bytes
content-length: ...
content-type: application/json
etag: ...
last-modified: ...
###
GET /index/3/b/bar.json
accept: */*
accept-encoding: gzip, br, deflate
host: ...
user-agent: ...
200 OK
accept-ranges: bytes
content-length: ...
content-type: application/json
etag: ...
last-modified: ...
###
GET /bar-1.0.0.tar.zst
accept: */*
accept-encoding: gzip, br, deflate
host: ...
user-agent: ...
200 OK
accept-ranges: bytes
content-length: ...
content-type: application/octet-stream
etag: ...
last-modified: ...
"]];
expected.assert_eq(&registry.logs());
}

#[test]
Expand Down Expand Up @@ -71,6 +119,34 @@ fn not_found() {
Caused by:
package not found in registry: baz ^1 (registry+http://[..])
"#});

let expected = expect![["
GET /config.json
accept: */*
accept-encoding: gzip, br, deflate
host: ...
user-agent: ...
200 OK
accept-ranges: bytes
content-length: ...
content-type: application/json
etag: ...
last-modified: ...
###
GET /index/3/b/baz.json
accept: */*
accept-encoding: gzip, br, deflate
host: ...
user-agent: ...
404 Not Found
content-length: 0
etag: ...
"]];
expected.assert_eq(&registry.logs());
}

#[test]
Expand Down Expand Up @@ -98,8 +174,21 @@ fn missing_config_json() {
0: failed to fetch registry config
1: HTTP status client error (404 Not Found) for url (http://[..]/config.json)
"#});

let expected = expect![["
GET /config.json
accept: */*
accept-encoding: gzip, br, deflate
host: ...
user-agent: ...
404 Not Found
content-length: 0
etag: ...
"]];
expected.assert_eq(&registry.logs());
}

// TODO(mkaput): Test errors properly when package is in index, but tarball is missing.
// TODO(mkaput): Test interdependencies.
// TODO(mkaput): Test offline mode.
// TODO(mkaput): Test offline mode, including with some cache prepopulated.
2 changes: 1 addition & 1 deletion utils/scarb-test-support/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn hang_on_tcp(args: HangOnTcpArgs) -> Result<()> {
#[tokio::main]
async fn http_server() -> Result<()> {
let http = SimpleHttpServer::serve(env::current_dir().unwrap());
http.log_requests(true);
http.print_logs(true);
println!("🚀 {}", http.url());
println!("Press enter to continue...");
let _ = io::stdin().read(&mut [0u8]).unwrap();
Expand Down
33 changes: 19 additions & 14 deletions utils/scarb-test-support/src/registry/http.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use assert_fs::fixture::ChildPath;
use assert_fs::prelude::*;
use std::fmt;
use std::path::Path;

use assert_fs::fixture::ChildPath;
use assert_fs::prelude::*;
use assert_fs::TempDir;
use once_cell::sync::Lazy;
use serde_json::json;
Expand All @@ -11,6 +11,16 @@ use tokio::runtime;
use crate::registry::local::LocalRegistry;
use crate::simple_http_server::SimpleHttpServer;

// Keep a global multi-threading runtime to contain all running servers in one shared
// thread pool, while maintaining synchronous nature of tests.
static RUNTIME: Lazy<runtime::Runtime> = Lazy::new(|| {
runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_all()
.build()
.unwrap()
});

pub struct HttpRegistry {
local: LocalRegistry,
url: String,
Expand All @@ -21,16 +31,6 @@ pub struct HttpRegistry {

impl HttpRegistry {
pub fn serve() -> Self {
// Keep a global multi-threading runtime to contain all running servers in one shared
// thread pool, while maintaining synchronous nature of tests.
static RUNTIME: Lazy<runtime::Runtime> = Lazy::new(|| {
runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_all()
.build()
.unwrap()
});

let local = LocalRegistry::create();
let server = {
let _guard = RUNTIME.enter();
Expand Down Expand Up @@ -58,8 +58,13 @@ impl HttpRegistry {
}

/// Enable this when writing tests to see what requests are being made in the test.
pub fn debug_log_requests(&self) {
self.server.log_requests(true);
pub fn print_logs(&self) {
self.server.print_logs(true);
}

pub fn logs(&self) -> String {
let _guard = RUNTIME.enter();
RUNTIME.block_on(async { self.server.logs_to_string().await })
}
}

Expand Down
Loading

0 comments on commit ecebc03

Please sign in to comment.