aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/repository/artifact_repository_test.rs
blob: 90e56257a3527a2117ce8f2dfc581b3ffdb64f78 (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
//! ref: composer/tests/Composer/Test/Repository/ArtifactRepositoryTest.php

use std::cell::RefCell;
use std::rc::Rc;

use indexmap::IndexMap;
use shirabe::io::{IOInterface, NullIO};
use shirabe::repository::ArtifactRepository;
use shirabe_php_shim::{PhpMixed, extension_loaded};

/// Returns true when the test should be skipped because the zip extension is
/// unavailable, mirroring PHP's markTestSkipped in setUp.
fn set_up() -> bool {
    if !extension_loaded("zip") {
        // markTestSkipped('You need the zip extension to run this test.')
        return true;
    }
    false
}

fn artifacts_dir() -> String {
    format!(
        "{}/../../composer/tests/Composer/Test/Repository/Fixtures/artifacts",
        env!("CARGO_MANIFEST_DIR")
    )
}

fn create_repo(url: &str) -> ArtifactRepository {
    let mut coordinates: IndexMap<String, PhpMixed> = IndexMap::new();
    coordinates.insert("type".to_string(), PhpMixed::String("artifact".to_string()));
    coordinates.insert("url".to_string(), PhpMixed::String(url.to_string()));

    let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(NullIO::new()));
    ArtifactRepository::new(coordinates, io).unwrap()
}

#[test]
#[ignore = "the artifacts fixtures dir contains a .tar file (jsonInRootTarFile); scanning it routes through Tar::get_composer_json -> PharData::new which is todo!()"]
fn test_extracts_configs_from_zip_archives() {
    if set_up() {
        return;
    }

    let mut expected_packages = vec![
        "vendor0/package0-0.0.1".to_string(),
        "composer/composer-1.0.0-alpha6".to_string(),
        "vendor1/package2-4.3.2".to_string(),
        "vendor3/package1-5.4.3".to_string(),
        "test/jsonInRoot-1.0.0".to_string(),
        "test/jsonInRootTarFile-1.0.0".to_string(),
        "test/jsonInFirstLevel-1.0.0".to_string(),
        // The files not-an-artifact.zip and jsonSecondLevel are not valid
        // artifacts and do not get detected.
    ];

    let mut repo = create_repo(&artifacts_dir());

    let mut found_packages: Vec<String> = repo
        .__get_packages()
        .unwrap()
        .iter()
        .map(|package| {
            format!(
                "{}-{}",
                package.get_pretty_name(),
                package.get_pretty_version()
            )
        })
        .collect();

    expected_packages.sort();
    found_packages.sort();

    assert_eq!(expected_packages, found_packages);

    let tar_package: Vec<_> = repo
        .__get_packages()
        .unwrap()
        .into_iter()
        .filter(|package| package.get_pretty_name() == "test/jsonInRootTarFile")
        .collect();
    assert_eq!(1, tar_package.len());
    let tar_package = tar_package.into_iter().next_back().unwrap();
    assert_eq!(Some("tar".to_string()), tar_package.get_dist_type());
}

#[test]
#[ignore = "the artifacts fixtures dir contains a .tar file (jsonInRootTarFile); scanning it routes through Tar::get_composer_json -> PharData::new which is todo!()"]
fn test_absolute_repo_url_creates_absolute_url_packages() {
    if set_up() {
        return;
    }

    let absolute_path = artifacts_dir();
    let mut repo = create_repo(&absolute_path);

    for package in repo.__get_packages().unwrap() {
        assert_eq!(
            package
                .get_dist_url()
                .unwrap_or_default()
                .find(&absolute_path.replace('\\', "/")),
            Some(0)
        );
    }
}

#[test]
#[ignore = "the relative url is resolved from the process cwd (the crate manifest dir under cargo, not the composer test root), so the artifacts dir is not found; additionally the dir contains a .tar file routing through PharData::new which is todo!()"]
fn test_relative_repo_url_creates_relative_url_packages() {
    if set_up() {
        return;
    }

    let relative_path = "tests/Composer/Test/Repository/Fixtures/artifacts";
    let mut repo = create_repo(relative_path);

    for package in repo.__get_packages().unwrap() {
        assert_eq!(
            package
                .get_dist_url()
                .unwrap_or_default()
                .find(relative_path),
            Some(0)
        );
    }
}