-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
175 additions
and
3 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/ortus/boxlang/runtime/bifs/global/io/DirectoryExists.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/test/java/ortus/boxlang/runtime/bifs/global/io/DirectoryExistsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters