Package spock.util.io

Class 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 with TempDir.
    Since:
    2.2
    • 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 for getCurrentPath().resolve(path)
        Parameters:
        path - the path to resolve relative to currentPath
        Returns:
        the resolved path
      • resolve

        public Path resolve​(Path path)
        A shorthand for getCurrentPath().resolve(path)
        Parameters:
        path - the path to resolve relative to currentPath
        Returns:
        the resolved path
      • 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 interface DirectoryFixture
        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 a dirSpec 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 interface DirectoryFixture
        Parameters:
        dir - path to the directory, can either be a single level or multilevel
        dirSpec - 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 interface DirectoryFixture
        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 interface DirectoryFixture
        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 interface DirectoryFixture
        Parameters:
        resourcePath - the path to the resource to load
        targetName - 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 interface DirectoryFixture
        Parameters:
        resourcePath - the path to the resource to load
        contextClass - 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 interface DirectoryFixture
        Parameters:
        resourcePath - the path to the resource to load
        targetName - the name of the target to use
        contextClass - 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