Skip to content

Commit

Permalink
[MENFORCER-364] requireFilesExist rule should be case sensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte committed Jul 23, 2021
1 parent faaf5c1 commit 335f26e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import java.io.File;
import java.io.IOException;

/**
* The Class RequireFilesExist.
Expand All @@ -31,7 +32,7 @@ public class RequireFilesExist
boolean checkFile( File file )
{
// if we get here and the handle is null, treat it as a success
return file == null ? true : file.exists();
return file == null ? true : file.exists() && osIndependentNameMatch( file, true );
}

@Override
Expand All @@ -40,4 +41,34 @@ String getErrorMsg()
return "Some required files are missing:" + System.lineSeparator();
}

/**
* OSes like Windows are case insensitive, so this method will compare the file path with the actual path. A simple
* {@link File#exists()} is not enough for such OS.
*
* @param file the file to verify
* @param defaultValue value to return in case an IO exception occurs, should never happen as the file already
* exists
* @return
*/
private boolean osIndependentNameMatch( File file, boolean defaultValue )
{
try
{
File absFile;
if ( !file.isAbsolute() )
{
absFile = new File( new File( "." ).getCanonicalFile(), file.getPath() );
}
else
{
absFile = file;
}

return absFile.toURI().equals( absFile.getCanonicalFile().toURI() );
}
catch ( IOException e )
{
return defaultValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import java.io.File;
import java.io.IOException;

import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.junit.Rule;
Expand All @@ -46,34 +45,43 @@ public class TestRequireFilesExist

@Test
public void testFileExists()
throws EnforcerRuleException, IOException
throws Exception
{
File f = temporaryFolder.newFile();

rule.setFiles( new File[] { f } );
rule.setFiles( new File[] { f.getCanonicalFile() } );

rule.execute( EnforcerTestUtils.getHelper() );
}

@Test
public void testFileOsIndependentExists()
throws Exception
{
rule.setFiles( new File[] { new File( "POM.xml" ) } );

EnforcerRuleException e =
assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) );

assertNotNull( e.getMessage() );
}


@Test
public void testEmptyFile()
throws EnforcerRuleException, IOException
throws Exception
{
rule.setFiles( new File[] { null } );
try
{
rule.execute( EnforcerTestUtils.getHelper() );
fail( "Should get exception" );
}
catch ( EnforcerRuleException e )
{
assertNotNull( e.getMessage() );
}

EnforcerRuleException e =
assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) );

assertNotNull( e.getMessage() );
}

@Test
public void testEmptyFileAllowNull()
throws EnforcerRuleException, IOException
throws Exception
{
rule.setFiles( new File[] { null } );
rule.setAllowNulls( true );
Expand All @@ -82,24 +90,21 @@ public void testEmptyFileAllowNull()

@Test
public void testEmptyFileList()
throws EnforcerRuleException, IOException
throws Exception
{
rule.setFiles( new File[] {} );
assertEquals( 0, rule.getFiles().length );
try
{
rule.execute( EnforcerTestUtils.getHelper() );
fail( "Should get exception" );
}
catch ( EnforcerRuleException e )
{
assertNotNull( e.getMessage() );
}

EnforcerRuleException e =
assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) );

assertNotNull( e.getMessage() );

}

@Test
public void testEmptyFileListAllowNull()
throws EnforcerRuleException, IOException
throws Exception
{
rule.setFiles( new File[] {} );
assertEquals( 0, rule.getFiles().length );
Expand All @@ -109,23 +114,19 @@ public void testEmptyFileListAllowNull()

@Test
public void testFileDoesNotExist()
throws EnforcerRuleException, IOException
throws Exception
{
File f = temporaryFolder.newFile();
f.delete();

assertFalse( f.exists() );
rule.setFiles( new File[] { f } );

try
{
rule.execute( EnforcerTestUtils.getHelper() );
fail( "Should get exception" );
}
catch ( EnforcerRuleException e )
{
assertNotNull( e.getMessage() );
}
EnforcerRuleException e =
assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) );

assertNotNull( e.getMessage() );

}

/**
Expand Down

0 comments on commit 335f26e

Please sign in to comment.