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 ac98b7f commit 588cb3b
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/main/java/ortus/boxlang/runtime/bifs/global/io/FileExists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

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 FileExists extends BIF {

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

/**
* Describe what the invocation of your bif function does
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
* @argument.foo Describe any expected arguments
*/
public Object invoke( IBoxContext context, ArgumentsScope arguments ) {
String filePath = arguments.getAsString( Key.source );
Boolean allowRealPath = arguments.getAsBoolean( Key.allowRealPath );

if ( !allowRealPath && Path.of( filePath ).isAbsolute() ) {
throw new BoxRuntimeException(
"The file or path argument [" + filePath + "] is an absolute path. This is disallowed when the allowRealPath argument is set to false."
);
}

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

}
2 changes: 2 additions & 0 deletions src/main/java/ortus/boxlang/runtime/scopes/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public class Key {
public static final Key charset = Key.of( "charset" );
public static final Key buffersize = Key.of( "buffersize" );
public static final Key charsetOrBufferSize = Key.of( "charsetOrBufferSize" );
public static final Key source = Key.of( "source" );
public static final Key allowRealPath = Key.of( "allowRealPath" );

// DateTime Common Argument Keys
public static final Key date = Key.of( "date" );
Expand Down
110 changes: 110 additions & 0 deletions src/test/java/ortus/boxlang/runtime/bifs/global/io/FileExistsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

/**
* [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 FileExistsTest {

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() {
instance.shutdown();
}

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

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

@DisplayName( "It tests the BIF FileExists on a non-existent file" )
@Test
public void testFileNotExists() throws IOException {
String testFile = "src/test/resources/tmp/not-exists.txt";
variables.put( Key.of( "testFile" ), Path.of( testFile ).toAbsolutePath().toString() );
instance.executeSource(
"""
result = fileExists( variables.testFile );
""",
context );
Boolean result = ( Boolean ) variables.get( Key.of( "result" ) );
assertFalse( result );
}

@DisplayName( "It tests the BIF FileExists on a directory" )
@Test
public void testDirectoryNotExists() throws IOException {
String testFile = "src/test/resources/tmp";
variables.put( Key.of( "testFile" ), Path.of( testFile ).toAbsolutePath().toString() );
instance.executeSource(
"""
result = fileExists( variables.testFile );
""",
context );
Boolean result = ( Boolean ) variables.get( Key.of( "result" ) );
assertFalse( result );
}

}

0 comments on commit 588cb3b

Please sign in to comment.