prek 0.3.11

A fast Git hook manager written in Rust, designed as a drop-in alternative to pre-commit, reimagined.
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use assert_fs::fixture::{FileWriteStr, PathChild, PathCreateDir};
use prek_consts::PRE_COMMIT_HOOKS_YAML;
use prek_consts::env_vars::EnvVars;

use crate::common::{TestContext, cmd_snapshot, git_cmd};

#[test]
fn language_version() {
    if !EnvVars::is_set(EnvVars::CI) {
        return;
    }

    let context = TestContext::new();
    context.init_project();

    context.write_pre_commit_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              # `major.minor` channel request.
              - id: channel
                name: channel
                language: dotnet
                entry: dotnet --version
                language_version: '10.0'
                always_run: true
                verbose: true
                pass_filenames: false

              # Omit language_version to use the default SDK selection.
              - id: default
                name: default
                language: dotnet
                entry: dotnet --version
                always_run: true
                verbose: true
                pass_filenames: false

              # TFM-style request should resolve to the matching SDK channel.
              - id: tfm
                name: tfm
                language: dotnet
                entry: dotnet --version
                language_version: 'net10.0'
                always_run: true
                verbose: true
                pass_filenames: false

              # Major-only request should resolve to the latest matching channel.
              - id: major
                name: major
                language: dotnet
                entry: dotnet --version
                language_version: '10'
                always_run: true
                verbose: true
                pass_filenames: false
    "});

    context.git_add(".");

    let filters: Vec<_> = context
        .filters()
        .into_iter()
        .chain([(r"\b(\d+\.\d+)\.\d+\b", "$1.X")])
        .collect();

    cmd_snapshot!(filters, context.run(), @"
    success: true
    exit_code: 0
    ----- stdout -----
    channel..................................................................Passed
    - hook id: channel
    - duration: [TIME]

      10.0.X
    default..................................................................Passed
    - hook id: default
    - duration: [TIME]

      10.0.X
    tfm......................................................................Passed
    - hook id: tfm
    - duration: [TIME]

      10.0.X
    major....................................................................Passed
    - hook id: major
    - duration: [TIME]

      10.0.X

    ----- stderr -----
    ");
}

/// Test invalid `language_version` format is rejected.
#[test]
fn invalid_language_version() {
    let context = TestContext::new();
    context.init_project();

    context.write_pre_commit_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              - id: local
                name: local
                language: dotnet
                entry: dotnet --version
                language_version: 'invalid-version'
                always_run: true
                verbose: true
                pass_filenames: false
    "});

    context.git_add(".");

    cmd_snapshot!(context.filters(), context.run(), @r"
    success: false
    exit_code: 2
    ----- stdout -----

    ----- stderr -----
    error: Failed to init hooks
      caused by: Invalid hook `local`
      caused by: Invalid `language_version` value: `invalid-version`
    ");
}

/// Test that multiple different SDK versions can coexist in the tool store.
/// `net10.0` is preinstalled in the CI, `net8.0` will be installed by the test.
#[test]
fn multiple_sdk_versions() -> anyhow::Result<()> {
    if !EnvVars::is_set(EnvVars::CI) {
        return Ok(());
    }

    let context = TestContext::new();
    context.init_project();

    context.write_pre_commit_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              - id: hook-8
                name: hook-8
                language: dotnet
                entry: dotnet --version
                language_version: '8.0'
                always_run: true
                pass_filenames: false
                verbose: true
              - id: hook-10
                name: hook-10
                language: dotnet
                entry: dotnet --version
                language_version: '10.0'
                always_run: true
                pass_filenames: false
                verbose: true
    "});
    context.git_add(".");

    let filters: Vec<_> = context
        .filters()
        .into_iter()
        .chain([(r"\b(\d+\.\d+)\.\d+\b", "$1.X")])
        .collect();

    cmd_snapshot!(filters, context.run(), @"
    success: true
    exit_code: 0
    ----- stdout -----
    hook-8...................................................................Passed
    - hook id: hook-8
    - duration: [TIME]

      8.0.X
    hook-10..................................................................Passed
    - hook id: hook-10
    - duration: [TIME]

      10.0.X

    ----- stderr -----
    ");

    // Verify only net8.0 SDK is installed in the tool store since net10.0 is preinstalled in the CI environment.
    // Path structure: [HOME]/tools/dotnet/[VERSION]/...
    let dotnet_tool_root = context.home_dir().child("tools").child("dotnet");

    let mut found_8 = false;
    let mut found_10 = false;

    for entry in std::fs::read_dir(dotnet_tool_root.path())?.flatten() {
        let name = entry.file_name().to_string_lossy().to_string();
        if name.starts_with('8') {
            found_8 = true;
        }
        if name.starts_with("10") {
            found_10 = true;
        }
    }

    assert!(found_8, "Managed dotnet 8.x should exist");
    assert!(
        !found_10,
        "dotnet 10.x should not be installed by the test since it's preinstalled in CI"
    );

    Ok(())
}

/// Test that `additional_dependencies` are installed correctly.
#[test]
fn additional_dependencies() {
    if !EnvVars::is_set(EnvVars::CI) {
        return;
    }

    let context = TestContext::new();
    context.init_project();

    context.write_pre_commit_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: local
                name: local
                language: dotnet
                entry: dotnet-outdated --version
                additional_dependencies: ["dotnet-outdated-tool"]
                always_run: true
                verbose: true
                pass_filenames: false
    "#});
    context.git_add(".");

    let filters: Vec<_> = context
        .filters()
        .into_iter()
        .chain([(r"\b(4\.7\.1\+)[0-9a-f]+\b", "${1}[SHA]")])
        .collect();

    cmd_snapshot!(filters.clone(), context.run(), @"
    success: true
    exit_code: 0
    ----- stdout -----
    local....................................................................Passed
    - hook id: local
    - duration: [TIME]

      A .NET Core global tool to list outdated Nuget packages.
      4.7.1+[SHA]

    ----- stderr -----
    ");

    // Run again to verify the `check_health` logic.
    cmd_snapshot!(filters, context.run(), @"
    success: true
    exit_code: 0
    ----- stdout -----
    local....................................................................Passed
    - hook id: local
    - duration: [TIME]

      A .NET Core global tool to list outdated Nuget packages.
      4.7.1+[SHA]

    ----- stderr -----
    ");
}

/// Test installing a specific version of a dotnet tool.
#[test]
fn additional_dependencies_with_version() {
    if !EnvVars::is_set(EnvVars::CI) {
        return;
    }

    let context = TestContext::new();
    context.init_project();

    context.write_pre_commit_config(indoc::indoc! {r#"
        repos:
          - repo: local
            hooks:
              - id: local
                name: local
                language: dotnet
                entry: dotnet-outdated --version
                additional_dependencies: ["dotnet-outdated-tool:4.7.1"]
                always_run: true
                verbose: true
                pass_filenames: false
    "#});
    context.git_add(".");

    let filters: Vec<_> = context
        .filters()
        .into_iter()
        .chain([(r"\b(4\.7\.1\+)[0-9a-f]+\b", "${1}[SHA]")])
        .collect();

    cmd_snapshot!(filters, context.run(), @"
    success: true
    exit_code: 0
    ----- stdout -----
    local....................................................................Passed
    - hook id: local
    - duration: [TIME]

      A .NET Core global tool to list outdated Nuget packages.
      4.7.1+[SHA]

    ----- stderr -----
    ");
}

/// Test that additional dependencies in a remote repo are installed correctly.
#[test]
fn additional_dependencies_in_remote_repo() -> anyhow::Result<()> {
    if !EnvVars::is_set(EnvVars::CI) {
        return Ok(());
    }

    let repo = TestContext::new();
    repo.init_project();

    let repo_path = repo.work_dir();
    repo_path
        .child(PRE_COMMIT_HOOKS_YAML)
        .write_str(indoc::indoc! {r#"
        - id: dotnet-outdated
          name: dotnet-outdated
          language: dotnet
          entry: dotnet-outdated --version
          additional_dependencies: ["dotnet-outdated-tool:4.7.1"]
    "#})?;
    repo.git_add(".");
    repo.git_commit("Add manifest");
    git_cmd(repo.work_dir())
        .args(["tag", "v0.1.0", "-m", "v0.1.0"])
        .output()?;

    let context = TestContext::new();
    context.init_project();
    context.write_pre_commit_config(&indoc::formatdoc! {r"
        repos:
          - repo: {}
            rev: v0.1.0
            hooks:
              - id: dotnet-outdated
                verbose: true
                pass_filenames: false
    ", repo_path.display()});

    context.git_add(".");

    let filters: Vec<_> = context
        .filters()
        .into_iter()
        .chain([(r"\b(4\.7\.1\+)[0-9a-f]+\b", "${1}[SHA]")])
        .collect();

    cmd_snapshot!(filters, context.run(), @"
    success: true
    exit_code: 0
    ----- stdout -----
    dotnet-outdated..........................................................Passed
    - hook id: dotnet-outdated
    - duration: [TIME]

      A .NET Core global tool to list outdated Nuget packages.
      4.7.1+[SHA]

    ----- stderr -----
    ");

    Ok(())
}

/// Ensure that stderr from hooks is captured and shown to the user.
#[test]
fn hook_stderr() -> anyhow::Result<()> {
    if !EnvVars::is_set(EnvVars::CI) {
        return Ok(());
    }

    let context = TestContext::new();
    context.init_project();

    context.write_pre_commit_config(indoc::indoc! {r"
        repos:
          - repo: local
            hooks:
              - id: local
                name: local
                language: dotnet
                entry: dotnet run --project ./hook
    "});

    // Create a minimal console app that writes to stderr
    context.work_dir().child("hook").create_dir_all()?;
    context
        .work_dir()
        .child("hook/hook.csproj")
        .write_str(indoc::indoc! {r#"
        <Project Sdk="Microsoft.NET.Sdk">
          <PropertyGroup>
            <OutputType>Exe</OutputType>
            <TargetFramework>net10.0</TargetFramework>
            <ImplicitUsings>disable</ImplicitUsings>
          </PropertyGroup>
        </Project>
    "#})?;
    context
        .work_dir()
        .child("hook/Program.cs")
        .write_str(indoc::indoc! {r#"
        using System;
        Console.Error.WriteLine("Error from hook");
        Console.Error.Flush();
        Environment.Exit(1);
    "#})?;

    context.git_add(".");

    cmd_snapshot!(context.filters(), context.run(), @"
    success: false
    exit_code: 1
    ----- stdout -----
    local....................................................................Failed
    - hook id: local
    - exit code: 1

      Error from hook

    ----- stderr -----
    ");

    Ok(())
}