Skip to content

Commit

Permalink
Preserve trailing slashes on expandPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed May 7, 2024
1 parent 8cd5605 commit 0a6c464
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package ortus.boxlang.runtime.bifs.global.io;

import java.io.File;

import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand Down Expand Up @@ -46,7 +48,17 @@ public ExpandPath() {
* include forward or backward slashes.
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
return FileSystemUtil.expandPath( context, arguments.getAsString( Key.path ) ).absolutePath().toString();
String path = arguments.getAsString( Key.path );
boolean hasTrailingSlash = path.endsWith( "/" ) || path.endsWith( "\\" );
String pathStr = FileSystemUtil.expandPath( context, path ).absolutePath().toString();

if ( hasTrailingSlash ) {
if ( !pathStr.endsWith( "/" ) || !pathStr.endsWith( "\\" ) ) {
pathStr += File.separator;
}
}
return pathStr;

}

}
11 changes: 2 additions & 9 deletions src/main/java/ortus/boxlang/runtime/util/FileSystemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,6 @@ public static String convertInputStreamToString( InputStream inputStream ) {
* @return The expanded path represented in a ResolvedFilePath record
*/
public static ResolvedFilePath expandPath( IBoxContext context, String path ) {
boolean hasTrailingSlash = path.endsWith( "/" ) || path.endsWith( "\\" );
// This really isn't a valid path, but ColdBox does this by carelessly appending too many slashes to view paths
if ( path.startsWith( "//" ) ) {
// strip one of them off
Expand Down Expand Up @@ -864,15 +863,9 @@ public static ResolvedFilePath expandPath( IBoxContext context, String path ) {
path = path.substring( matchingMappingEntry.getKey().getName().length() );
String matchingMapping = matchingMappingEntry.getValue().toString();
Path result = Path.of( matchingMapping, path ).toAbsolutePath();
String pathStr = result.toString();
// Ensure we keep any original trailing slash
if ( hasTrailingSlash ) {
if ( !pathStr.endsWith( "/" ) || !pathStr.endsWith( "\\" ) ) {
pathStr += File.separator;
}
}

return ResolvedFilePath.of( matchingMappingEntry.getKey().getName(), matchingMapping, Path.of( finalPath ).normalize().toString(),
Path.of( pathStr ).normalize().toString() );
result.normalize() );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static com.google.common.truth.Truth.assertThat;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -176,9 +177,21 @@ public void testCanonicalize() {
context );
assertThat( variables.get( Key.of( "result1" ) ) ).isEqualTo( rootMapping );
assertThat( variables.get( Key.of( "result2" ) ) ).isEqualTo( parentOfRootMappings );
assertThat( variables.get( Key.of( "result3" ) ) ).isEqualTo( rootMapping );
assertThat( variables.get( Key.of( "result4" ) ) ).isEqualTo( parentOfRootMappings );
assertThat( variables.get( Key.of( "result3" ) ) ).isEqualTo( rootMapping + File.separator );
assertThat( variables.get( Key.of( "result4" ) ) ).isEqualTo( parentOfRootMappings + File.separator );
assertThat( variables.get( Key.of( "result5" ) ) ).isEqualTo( rootMapping );
assertThat( variables.get( Key.of( "result6" ) ) ).isEqualTo( parentOfParentOfRootMappings );
}

@Test
public void testTrailingSlash() {
instance.executeSource(
"""
result = expandPath('/some/path/')
result2 = expandPath('/some/path')
""",
context );
assertThat( variables.getAsString( result ).endsWith( File.separator ) ).isTrue();
assertThat( variables.getAsString( Key.of( "result2" ) ).endsWith( File.separator ) ).isFalse();
}
}

0 comments on commit 0a6c464

Please sign in to comment.