Skip to content

Commit

Permalink
implement FileExists BIF and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jclausen committed Jan 5, 2024
1 parent 588cb3b commit a20f160
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

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

import java.nio.file.Files;
import java.nio.file.Path;

import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.scopes.ArgumentsScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.types.Argument;
import ortus.boxlang.runtime.types.exceptions.BoxRuntimeException;
import ortus.boxlang.runtime.util.FileSystemUtil;

@BoxBIF

public class DirectoryExists extends BIF {

/**
* Constructor
*/
public DirectoryExists() {
super();
declaredArguments = new Argument[] {
new Argument( true, "string", Key.path ),
new Argument( true, "boolean", Key.allowRealPath, true )
};
}

/**
* Determines whether a directory exists
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
* @argument.path The directory path
*
* @arguments.allowRealPath Whether to allow an absolute path as the path argument
*/
public Object invoke( IBoxContext context, ArgumentsScope arguments ) {
String directoryPath = arguments.getAsString( Key.path );
Boolean allowRealPath = arguments.getAsBoolean( Key.allowRealPath );
if ( !allowRealPath && Path.of( directoryPath ).isAbsolute() ) {
throw new BoxRuntimeException(
"The file or path argument [" + directoryPath + "] is an absolute path. This is disallowed when the allowRealPath argument is set to false."
);
}

return ( Boolean ) FileSystemUtil.exists( directoryPath ) && Files.isDirectory( Path.of( directoryPath ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ public FileExists() {
}

/**
* Describe what the invocation of your bif function does
* Determines whether a file exists
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
* @argument.foo Describe any expected arguments
* @argument.source The file path
*
* @arguments.allowRealPath Whether to allow an absolute path as the path argument
*/
public Object invoke( IBoxContext context, ArgumentsScope arguments ) {
String filePath = arguments.getAsString( Key.source );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@

/**
* [BoxLang]
*
* Copyright [2023] [Ortus Solutions, Corp]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Path;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.context.ScriptingBoxContext;
import ortus.boxlang.runtime.scopes.IScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.scopes.VariablesScope;
import ortus.boxlang.runtime.util.FileSystemUtil;

public class DirectoryExistsTest {

static BoxRuntime instance;
static IBoxContext context;
static IScope variables;
static Key result = new Key( "result" );

@BeforeAll
public static void setUp() {
instance = BoxRuntime.getInstance( true );
context = new ScriptingBoxContext( instance.getRuntimeContext() );
variables = context.getScopeNearby( VariablesScope.name );
}

@AfterAll
public static void teardown() throws IOException {
FileSystemUtil.deleteDirectory( "src/test/resources/tmp", true );
instance.shutdown();
}

@BeforeEach
public void setupEach() {
variables.clear();
}

@DisplayName( "It tests the BIF DirectoryExists on an existing directory" )
@Test
public void testDirectoryExists() throws IOException {
String testDirectory = "src/test/resources/tmp/foo";
variables.put( Key.of( "testDirectory" ), Path.of( testDirectory ).toAbsolutePath().toString() );
FileSystemUtil.createDirectory( testDirectory );
assertTrue( FileSystemUtil.exists( testDirectory ) );
instance.executeSource(
"""
result = DirectoryExists( variables.testDirectory );
""",
context );
Boolean result = ( Boolean ) variables.get( Key.of( "result" ) );
assertTrue( result );
}

@DisplayName( "It tests the BIF DirectoryExists on a non-existent directory" )
@Test
public void testDirectoryNotExists() throws IOException {
String testDirectory = "src/test/resources/tmp/blah";
if ( FileSystemUtil.exists( testDirectory ) ) {
FileSystemUtil.deleteDirectory( testDirectory, true );
}
variables.put( Key.of( "testDirectory" ), Path.of( testDirectory ).toAbsolutePath().toString() );
instance.executeSource(
"""
result = DirectoryExists( variables.testDirectory );
""",
context );
Boolean result = ( Boolean ) variables.get( Key.of( "result" ) );
assertFalse( result );
}

@DisplayName( "It tests the BIF DirectoryExists on a file" )
@Test
public void testDirectoryExistsFile() throws IOException {
String testFile = "src/test/resources/tmp/a-file.txt";
variables.put( Key.of( "testFile" ), Path.of( testFile ).toAbsolutePath().toString() );
FileSystemUtil.write( testFile, "test directory!".getBytes( "UTF-8" ), true );
assertTrue( FileSystemUtil.exists( testFile ) );
instance.executeSource(
"""
result = DirectoryExists( variables.testFile );
""",
context );
Boolean result = ( Boolean ) variables.get( Key.of( "result" ) );
assertFalse( result );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public static void setUp() {
}

@AfterAll
public static void teardown() {
public static void teardown() throws IOException {
FileSystemUtil.deleteDirectory( "src/test/resources/tmp", true );
instance.shutdown();
}

Expand Down

0 comments on commit a20f160

Please sign in to comment.