Skip to content

Commit a1ad3f4

Browse files
committed
Add tests for HierarchicalOutputDirectoryProvider
1 parent cd08588 commit a1ad3f4

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/HierarchicalOutputDirectoryProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Path createOutputDirectory(TestDescriptor testDescriptor) throws IOExcept
4747
.skip(1) //
4848
.map(Segment::getValue) //
4949
.map(HierarchicalOutputDirectoryProvider::sanitizeName).map(Paths::get) //
50-
.reduce(Paths.get(firstSegment.getValue()), Path::resolve);
50+
.reduce(Paths.get(sanitizeName(firstSegment.getValue())), Path::resolve);
5151
return Files.createDirectories(getRootDirectory().resolve(relativePath));
5252
}
5353

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.launcher.core;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.mockito.Mockito.times;
15+
import static org.mockito.Mockito.verify;
16+
import static org.mockito.Mockito.when;
17+
18+
import java.nio.file.Path;
19+
import java.util.function.Supplier;
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.io.TempDir;
24+
import org.junit.platform.engine.TestDescriptor;
25+
import org.junit.platform.engine.UniqueId;
26+
import org.mockito.InjectMocks;
27+
import org.mockito.Mock;
28+
import org.mockito.junit.jupiter.MockitoSettings;
29+
30+
@MockitoSettings
31+
public class HierarchicalOutputDirectoryProviderTests {
32+
33+
@TempDir
34+
Path tempDir;
35+
36+
@Mock
37+
Supplier<Path> rootDirSupplier;
38+
39+
@Mock
40+
TestDescriptor testDescriptor;
41+
42+
@InjectMocks
43+
HierarchicalOutputDirectoryProvider provider;
44+
45+
@BeforeEach
46+
void prepareMock() {
47+
when(rootDirSupplier.get()).thenReturn(tempDir);
48+
}
49+
50+
@Test
51+
void returnsConfiguredRootDir() {
52+
assertThat(provider.getRootDirectory()).isEqualTo(tempDir);
53+
assertThat(provider.getRootDirectory()).isEqualTo(tempDir);
54+
verify(rootDirSupplier, times(1)).get();
55+
}
56+
57+
@Test
58+
void createsSubDirectoriesBasedOnUniqueId() throws Exception {
59+
var uniqueId = UniqueId.forEngine("engine") //
60+
.append("irrelevant", "foo") //
61+
.append("irrelevant", "bar");
62+
when(testDescriptor.getUniqueId()).thenReturn(uniqueId);
63+
64+
var outputDir = provider.createOutputDirectory(testDescriptor);
65+
66+
assertThat(outputDir) //
67+
.isEqualTo(tempDir.resolve(Path.of("engine", "foo", "bar"))) //
68+
.exists();
69+
}
70+
71+
@Test
72+
void replacesForbiddenCharacters() throws Exception {
73+
var uniqueId = UniqueId.forEngine("engine<>") //
74+
.append("irrelevant", "*/abc");
75+
when(testDescriptor.getUniqueId()).thenReturn(uniqueId);
76+
77+
var outputDir = provider.createOutputDirectory(testDescriptor);
78+
79+
assertThat(outputDir) //
80+
.isEqualTo(tempDir.resolve(Path.of("engine__", "__abc"))) //
81+
.exists();
82+
}
83+
}

0 commit comments

Comments
 (0)