aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/command/init_command_test.rs
blob: 6b118f3e52721d1e49216aca4ed8b18e848b89a3 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! ref: composer/tests/Composer/Test/Command/InitCommandTest.php

// The run cases (testRunCommand, testRunCommandInvalid, testRunGuessNameFromDirSanitizesDir,
// testInteractiveRun) drive the full command via ApplicationTester / initTempComposer, which
// does not exist here; they remain reason'd-ignore. The unit-style cases call the helper
// methods directly via `__`-prefixed test-only wrappers.

use shirabe::command::init_command::InitCommand;
use shirabe_php_shim::{PhpMixed, server_set};
use tempfile::TempDir;

fn set_up() {
    server_set("COMPOSER_DEFAULT_AUTHOR", "John Smith".to_string());
    server_set("COMPOSER_DEFAULT_EMAIL", "john@example.com".to_string());
}

/// @return iterable<string, array{0: string, 1: string|null, 2: string}>
fn valid_author_string_provider() -> Vec<(&'static str, Option<&'static str>, &'static str)> {
    vec![
        // simple
        (
            "John Smith",
            Some("john@example.com"),
            "John Smith <john@example.com>",
        ),
        // without email
        ("John Smith", None, "John Smith"),
        // UTF-8
        (
            "Matti Meikäläinen",
            Some("matti@example.com"),
            "Matti Meikäläinen <matti@example.com>",
        ),
        // UTF-8 with non-spacing marks (\xCC\x88 is U+0308 combining diaeresis)
        (
            "Matti Meika\u{0308}la\u{0308}inen",
            Some("matti@example.com"),
            "Matti Meika\u{0308}la\u{0308}inen <matti@example.com>",
        ),
        // numeric author name
        ("h4x0r", Some("h4x@example.com"), "h4x0r <h4x@example.com>"),
        // alias 1 (issue #5631)
        (
            "Johnathon \"Johnny\" Smith",
            Some("john@example.com"),
            "Johnathon \"Johnny\" Smith <john@example.com>",
        ),
        // alias 2 (issue #5631)
        (
            "Johnathon (Johnny) Smith",
            Some("john@example.com"),
            "Johnathon (Johnny) Smith <john@example.com>",
        ),
    ]
}

#[ignore]
#[test]
fn test_parse_valid_author_string() {
    set_up();

    for (name, email, input) in valid_author_string_provider() {
        let command = InitCommand::new();
        let author = command.__parse_author_string(input).unwrap();
        assert_eq!(
            Some(name.to_string()),
            author.get("name").cloned().flatten()
        );
        assert_eq!(
            email.map(|e| e.to_string()),
            author.get("email").cloned().flatten()
        );
    }
}

#[ignore]
#[test]
fn test_parse_empty_author_string() {
    set_up();

    let command = InitCommand::new();
    let result = command.__parse_author_string("");
    assert!(result.is_err());
}

#[ignore]
#[test]
fn test_parse_author_string_with_invalid_email() {
    set_up();

    let command = InitCommand::new();
    let result = command.__parse_author_string("John Smith <john>");
    assert!(result.is_err());
}

#[test]
fn test_namespace_from_valid_package_name() {
    set_up();

    let command = InitCommand::new();
    let namespace = command.namespace_from_package_name("new_projects.acme-extra/package-name");
    assert_eq!(
        Some("NewProjectsAcmeExtra\\PackageName".to_string()),
        namespace
    );
}

#[test]
fn test_namespace_from_invalid_package_name() {
    set_up();

    let command = InitCommand::new();
    let namespace = command.namespace_from_package_name("invalid-package-name");
    assert_eq!(None, namespace);
}

#[test]
fn test_namespace_from_missing_package_name() {
    set_up();

    let command = InitCommand::new();
    let namespace = command.namespace_from_package_name("");
    assert_eq!(None, namespace);
}

#[ignore = "needs TestCase::init_temp_composer and get_application_tester (ApplicationTester) infrastructure, not implemented"]
#[test]
fn test_run_command() {
    set_up();

    todo!()
}

#[ignore = "needs TestCase::init_temp_composer and get_application_tester (ApplicationTester) infrastructure, not implemented"]
#[test]
fn test_run_command_invalid() {
    set_up();

    todo!()
}

#[ignore = "needs TestCase::init_temp_composer and get_application_tester (ApplicationTester) infrastructure, not implemented"]
#[test]
fn test_run_guess_name_from_dir_sanitizes_dir() {
    set_up();

    todo!()
}

#[ignore = "needs TestCase::init_temp_composer and get_application_tester (ApplicationTester with set_inputs) infrastructure, not implemented"]
#[test]
fn test_interactive_run() {
    set_up();

    todo!()
}

#[ignore]
#[test]
fn test_format_authors() {
    set_up();

    let author_with_email = "John Smith <john@example.com>";
    let author_without_email = "John Smith";
    let command = InitCommand::new();

    let authors = command.__format_authors(author_with_email).unwrap();
    let mut expected: indexmap::IndexMap<String, PhpMixed> = indexmap::IndexMap::new();
    expected.insert(
        "name".to_string(),
        PhpMixed::String("John Smith".to_string()),
    );
    expected.insert(
        "email".to_string(),
        PhpMixed::String("john@example.com".to_string()),
    );
    assert_eq!(expected, authors[0]);

    let authors = command.__format_authors(author_without_email).unwrap();
    let mut expected: indexmap::IndexMap<String, PhpMixed> = indexmap::IndexMap::new();
    expected.insert(
        "name".to_string(),
        PhpMixed::String("John Smith".to_string()),
    );
    assert_eq!(expected, authors[0]);
}

#[ignore]
#[test]
fn test_get_git_config() {
    set_up();

    let mut command = InitCommand::new();
    let git_config = command.__get_git_config();
    assert!(git_config.contains_key("user.name"));
    assert!(git_config.contains_key("user.email"));
}

#[ignore]
#[test]
fn test_add_vendor_ignore() {
    set_up();

    let tmp = TempDir::new().unwrap();
    let ignore_file = tmp.path().join("ignore");
    let ignore_file = ignore_file.to_str().unwrap();

    let command = InitCommand::new();
    command.__add_vendor_ignore(ignore_file, "/vendor/");
    assert!(std::path::Path::new(ignore_file).exists());
    let content = std::fs::read_to_string(ignore_file).unwrap();
    assert!(content.contains("/vendor/"));
}

#[ignore]
#[test]
fn test_has_vendor_ignore() {
    set_up();

    let tmp = TempDir::new().unwrap();
    let ignore_file = tmp.path().join("ignore");
    let ignore_file = ignore_file.to_str().unwrap();

    let command = InitCommand::new();
    assert!(!command.__has_vendor_ignore(ignore_file, "vendor"));
    command.__add_vendor_ignore(ignore_file, "/vendor/");
    assert!(command.__has_vendor_ignore(ignore_file, "vendor"));
}