-
-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathupload.rs
More file actions
83 lines (77 loc) · 3.14 KB
/
upload.rs
File metadata and controls
83 lines (77 loc) · 3.14 KB
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
use std::sync::atomic::{AtomicU8, Ordering};
use crate::integration::{AssertCommand, MockEndpointBuilder, TestManager};
#[test]
fn command_code_mappings_upload() {
TestManager::new()
.mock_endpoint(
MockEndpointBuilder::new("POST", "/api/0/organizations/wat-org/code-mappings/bulk/")
.with_response_file("code_mappings/post-bulk.json"),
)
.register_trycmd_test("code_mappings/code-mappings-upload.trycmd")
.with_default_token();
}
#[test]
fn command_code_mappings_upload_partial_error() {
TestManager::new()
.mock_endpoint(
MockEndpointBuilder::new("POST", "/api/0/organizations/wat-org/code-mappings/bulk/")
.with_response_file("code_mappings/post-bulk-partial-error.json"),
)
.register_trycmd_test("code_mappings/code-mappings-upload-partial-error.trycmd")
.with_default_token();
}
#[test]
fn command_code_mappings_upload_batches() {
// Generate a fixture with 301 mappings to force 2 batches (300 + 1).
let mut mappings = Vec::with_capacity(301);
for i in 0..301 {
mappings.push(serde_json::json!({
"stackRoot": format!("com/example/m{i}"),
"sourceRoot": format!("modules/m{i}/src/main/java/com/example/m{i}"),
}));
}
let fixture = tempfile::NamedTempFile::new().expect("failed to create temp file");
serde_json::to_writer(&fixture, &mappings).expect("failed to write fixture");
let call_count = AtomicU8::new(0);
TestManager::new()
.mock_endpoint(
MockEndpointBuilder::new("POST", "/api/0/organizations/wat-org/code-mappings/bulk/")
.expect(2)
.with_response_fn(move |_request| {
let n = call_count.fetch_add(1, Ordering::Relaxed);
// Return appropriate counts per batch
let (created, mapping_count) = if n == 0 { (300, 300) } else { (1, 1) };
let mut batch_mappings = Vec::new();
for i in 0..mapping_count {
let idx = n as usize * 300 + i;
batch_mappings.push(serde_json::json!({
"stackRoot": format!("com/example/m{idx}"),
"sourceRoot": format!("modules/m{idx}/src/main/java/com/example/m{idx}"),
"status": "created",
}));
}
serde_json::to_vec(&serde_json::json!({
"created": created,
"updated": 0,
"errors": 0,
"mappings": batch_mappings,
}))
.expect("failed to serialize response")
}),
)
.assert_cmd([
"code-mappings",
"upload",
fixture.path().to_str().expect("valid utf-8 path"),
"--org",
"wat-org",
"--project",
"wat-project",
"--repo",
"owner/repo",
"--default-branch",
"main",
])
.with_default_token()
.run_and_assert(AssertCommand::Success);
}