Skip to content

Commit c068b12

Browse files
spannmslawekjaranowski
authored andcommitted
[SUREFIRE-2109] Add suffix derived from current user to Surefire temp directory name
1 parent 0097a41 commit c068b12

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

surefire-api/src/main/java/org/apache/maven/surefire/api/util/SureFireFileManager.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,47 @@
2020
*/
2121

2222
import java.io.File;
23+
import java.util.Objects;
24+
import java.util.stream.Stream;
2325

2426
/**
25-
* Centralized file management of surefire.
27+
* Centralized file management of temporary files in surefire.<br>
28+
* Files are deleted on VM exit.
2629
*
2730
* @author Markus Spann
2831
*/
2932
public final class SureFireFileManager
3033
{
3134

32-
public static File createTempFile( String prefix, String suffix )
35+
private static TempFileManager instance = create();
36+
37+
private static TempFileManager create()
3338
{
39+
String subDirName = "surefire";
3440

35-
return TempFileManager.instance( "surefire" ).createTempFile( prefix, suffix );
41+
// create directory name suffix from legal chars in the current user name
42+
// or a millisecond timestamp as fallback
43+
String userSuffix = Stream.of( "user.name", "USER", "USERNAME" )
44+
.map( System::getProperty )
45+
.filter( Objects::nonNull )
46+
.findFirst()
47+
.map( u -> u.replaceAll( "[^A-Za-z0-9\\-_]", "" ) )
48+
.map( u -> u.isEmpty() ? null : u )
49+
.orElse( Long.toString( System.currentTimeMillis() ) );
3650

51+
if ( userSuffix != null )
52+
{
53+
subDirName += "-" + userSuffix;
54+
}
55+
56+
TempFileManager tfm = TempFileManager.instance( subDirName );
57+
tfm.setDeleteOnExit( true );
58+
return tfm;
59+
}
60+
61+
public static File createTempFile( String prefix, String suffix )
62+
{
63+
return instance.createTempFile( prefix, suffix );
3764
}
3865

3966
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.apache.maven.surefire.api.util;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
import junit.framework.TestCase;
25+
import org.junit.Test;
26+
27+
import java.io.File;
28+
import java.io.IOException;
29+
import java.nio.file.FileSystems;
30+
import java.nio.file.Files;
31+
import java.nio.file.attribute.PosixFilePermission;
32+
import java.nio.file.attribute.PosixFilePermissions;
33+
import java.util.Set;
34+
35+
/**
36+
* Unit test for the surefire instance of temp file manager.
37+
*
38+
* @author Markus Spann
39+
*/
40+
public class SureFireFileManagerTest extends TestCase
41+
{
42+
43+
@Test
44+
public void testCreateTempFile() throws IOException
45+
{
46+
47+
File tempFile = SureFireFileManager.createTempFile( "sfprefix", "sfsuffix" );
48+
assertThat( tempFile ).isWritable();
49+
assertThat( tempFile.getName() ).startsWith( "sfprefix" ).endsWith( "sfsuffix" );
50+
51+
File tempDir = tempFile.getParentFile();
52+
assertThat( tempDir ).isDirectory().isWritable();
53+
assertThat( tempDir.getName() ).startsWith( "surefire-" ).doesNotMatch( "[^A-Za-z0-9\\\\-_]" );
54+
55+
boolean isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains( "posix" );
56+
if ( isPosix )
57+
{
58+
Set<PosixFilePermission> permissions = Files.getPosixFilePermissions( tempDir.toPath() );
59+
assertEquals( "rwxrwxr-x", PosixFilePermissions.toString( permissions ) );
60+
}
61+
62+
}
63+
64+
}

0 commit comments

Comments
 (0)