Package spock.util.io
Class FileSystemFixture
- java.lang.Object
-
- spock.util.io.FileSystemFixture
-
- All Implemented Interfaces:
DirectoryFixture
@Beta public class FileSystemFixture extends Object implements DirectoryFixture
FileSystemFixture can be used to create temporary directories and files. It is intended to be used withTempDir
.- Since:
- 2.2
-
-
Constructor Summary
Constructors Constructor Description FileSystemFixture(Path currentPath)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Path
copyFromClasspath(String resourcePath)
Copies a file from classpath using the contextClass to load the resource.Path
copyFromClasspath(String resourcePath, Class<?> contextClass)
Copies a file from classpath using the contextClass to load the resource.Path
copyFromClasspath(String resourcePath, String targetName)
Copies a file from classpath using the contextClass to load the resource.Path
copyFromClasspath(String resourcePath, String targetName, Class<?> contextClass)
Copies a file from classpath using the contextClass to load the resource.void
create(Closure<?> dirSpec)
Basically the same asDirectoryFixture.dir(String, Closure)
, but with the current root as base.Path
dir(String dir)
Creates a directory, creates the ancestors as necessary.Path
dir(String dir, Closure<?> dirSpec)
Creates a directory, creates the ancestors as necessary.Path
file(String file)
Creates a file object relative to the enclosing fixture or directory, but not the physical File, it will eagerly create parent directories if necessary.Path
getCurrentPath()
Path
resolve(String path)
A shorthand forgetCurrentPath().resolve(path)
Path
resolve(Path path)
A shorthand forgetCurrentPath().resolve(path)
-
-
-
Constructor Detail
-
FileSystemFixture
public FileSystemFixture(Path currentPath)
-
-
Method Detail
-
getCurrentPath
public Path getCurrentPath()
- Returns:
- the path of the this fixture
-
resolve
public Path resolve(String path)
A shorthand forgetCurrentPath().resolve(path)
- Parameters:
path
- the path to resolve relative to currentPath- Returns:
- the resolved path
-
resolve
public Path resolve(Path path)
A shorthand forgetCurrentPath().resolve(path)
- Parameters:
path
- the path to resolve relative to currentPath- Returns:
- the resolved path
-
create
public void create(@DelegatesTo(value=DirectoryFixture.class,strategy=1) Closure<?> dirSpec) throws IOException
Basically the same asDirectoryFixture.dir(String, Closure)
, but with the current root as base.- Throws:
IOException
-
dir
public Path dir(String dir) throws IOException
Description copied from interface:DirectoryFixture
Creates a directory, creates the ancestors as necessary.- Specified by:
dir
in interfaceDirectoryFixture
- Parameters:
dir
- path to the directory, can either be a single level or multilevel- Returns:
- the path to the directory
- Throws:
IOException
- if the directories could not be created.
-
dir
public Path dir(String dir, Closure<?> dirSpec) throws IOException
Description copied from interface:DirectoryFixture
Creates a directory, creates the ancestors as necessary. Accepts adirSpec
closure, that can create files and directories in the scope of this one. Example:dir('src') { dir('main') { dir('groovy') { file('HelloWorld.java') << 'println "Hello World"' } } dir('test/resources') { file('META-INF/MANIFEST.MF') << 'bogus entry' } }
- Specified by:
dir
in interfaceDirectoryFixture
- Parameters:
dir
- path to the directory, can either be a single level or multileveldirSpec
- a closure within the scope of this directory.- Returns:
- the path to the directory
- Throws:
IOException
- if the directories could not be created.
-
file
public Path file(String file) throws IOException
Description copied from interface:DirectoryFixture
Creates a file object relative to the enclosing fixture or directory, but not the physical File, it will eagerly create parent directories if necessary.- Specified by:
file
in interfaceDirectoryFixture
- Parameters:
file
- the (path and) filename- Returns:
- the
Path
object pointing to the file - Throws:
IOException
- if the parent directories could not be created.
-
copyFromClasspath
public Path copyFromClasspath(String resourcePath) throws IOException
Description copied from interface:DirectoryFixture
Copies a file from classpath using the contextClass to load the resource.This will use the
Closure.getOwner()
of the enclosing closure to determine the contextClass.The target name will be the same as the last path element of the
resourcePath
.- Specified by:
copyFromClasspath
in interfaceDirectoryFixture
- Parameters:
resourcePath
- the path to the resource to load- Returns:
- the
Path
pointing to the copied file - Throws:
IOException
- if the parent directories could not be created or the resource could not be found
-
copyFromClasspath
public Path copyFromClasspath(String resourcePath, String targetName) throws IOException
Description copied from interface:DirectoryFixture
Copies a file from classpath using the contextClass to load the resource.This will use the
Closure.getOwner()
of the enclosing closure to determine the contextClass.- Specified by:
copyFromClasspath
in interfaceDirectoryFixture
- Parameters:
resourcePath
- the path to the resource to loadtargetName
- the name of the target to use- Returns:
- the
Path
pointing to the copied file - Throws:
IOException
- if the parent directories could not be created or the resource could not be found
-
copyFromClasspath
public Path copyFromClasspath(String resourcePath, Class<?> contextClass) throws IOException
Description copied from interface:DirectoryFixture
Copies a file from classpath using the contextClass to load the resource.The target name will be the same as the last path element of the
resourcePath
.- Specified by:
copyFromClasspath
in interfaceDirectoryFixture
- Parameters:
resourcePath
- the path to the resource to loadcontextClass
- the class to use to load the resource- Returns:
- the
Path
pointing to the copied file - Throws:
IOException
- if the parent directories could not be created or the resource could not be found
-
copyFromClasspath
public Path copyFromClasspath(String resourcePath, String targetName, Class<?> contextClass) throws IOException
Description copied from interface:DirectoryFixture
Copies a file from classpath using the contextClass to load the resource.- Specified by:
copyFromClasspath
in interfaceDirectoryFixture
- Parameters:
resourcePath
- the path to the resource to loadtargetName
- the name of the target to usecontextClass
- the class to use to load the resource- Returns:
- the
Path
pointing to the copied file - Throws:
IOException
- if the parent directories could not be created or the resource could not be found
-
-