Skip to content

Commit 81dfd59

Browse files
authored
Merge pull request #14 from agorapulse/feature/fixt-copy-dir
[Fixt] Directory copy
2 parents 11a67b9 + ce40af9 commit 81dfd59

9 files changed

Lines changed: 67 additions & 6 deletions

File tree

.asciidoctorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:root-dir: {asciidoctorconfigdir}

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ jobs:
5353
token: ${{ secrets.AGORAPULSE_BOT_PERSONAL_TOKEN }}
5454
repository: ${{ matrix.repository }}
5555
event-type: ap-new-version-released-event
56-
client-payload: '{ "group": "com.agorapulse", "module": "notification", "version": "${{ steps.version.outputs.tag }}", "property" : "testing.libraries.version", "github" : ${{ toJson(github) }} }'
56+
client-payload: '{ "group": "com.agorapulse.testing", "module": "testing-libraries", "version": "${{ steps.version.outputs.tag }}", "property" : "testing.libraries.version", "github" : ${{ toJson(github) }} }'

build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,14 @@ config {
9696

9797
allprojects {
9898
repositories {
99-
jcenter()
10099
mavenCentral()
101-
maven { url "https://dl.bintray.com/agorapulse/libs" }
102100
maven { url "https://repo.spring.io/release" }
103101
maven { url "https://repo.grails.org/grails/core" }
104102
}
105103

106104
license {
107105
exclude '**/*.json'
106+
exclude '**/*.txt'
108107
exclude '***.yml'
109108
exclude '**/FixtSpec/*.*'
110109
exclude '**/ReferenceClass/*.*'

docs/guide/src/docs/asciidoc/index.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ include::{root-dir}/libs/fixt/src/test/groovy/com/agorapulse/testing/fixt/FixtSp
7373
<5> Create all necessary directories if they do not exist yet
7474
<6> Write the content of the stream into a file
7575
<7> Write the text into a file
76+
<8> Copy whole directory from the test resources
77+
<9> All thehe files from the fixture directory are available in the new destination
7678

7779
== Grails Job Testing Support
7880

libs/fixt/src/main/groovy/com/agorapulse/testing/fixt/Fixt.java

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.nio.charset.StandardCharsets;
27-
import java.nio.file.FileSystems;
28-
import java.nio.file.Files;
29-
import java.nio.file.StandardCopyOption;
27+
import java.nio.file.*;
28+
import java.nio.file.attribute.BasicFileAttributes;
3029
import java.util.ArrayList;
3130
import java.util.Arrays;
3231
import java.util.Collections;
@@ -157,6 +156,35 @@ public String readText(String fileName) {
157156
return streamToText(stream);
158157
}
159158

159+
/**
160+
* Copies all files from the fixtureDirectoryPath into destinationDirectory.
161+
*
162+
* <p>
163+
* The files are copied from the test resources folder such as <code>src/main/test/resources</code>.
164+
* The current implementation does not support reading the directory content from the classpath (e.g. a different project)
165+
* </p>
166+
* @param fixtureDirectoryPath
167+
* @param destinationDirectory
168+
* @return
169+
*/
170+
public File copyTo(String fixtureDirectoryPath, File destinationDirectory) {
171+
File testingDirectory = new File(getTestResourcesLocation(), getFixtureLocation(fixtureDirectoryPath));
172+
173+
if (!testingDirectory.exists()) {
174+
if (testingDirectory.mkdirs()) {
175+
throw new IllegalArgumentException("The directory " + testingDirectory.getAbsolutePath() + " does not exist but it has been crated!"
176+
+ "Please, pay attention that the folder must exists in the very same project as the test");
177+
}
178+
}
179+
180+
try {
181+
copyFolder(testingDirectory.toPath(), destinationDirectory.toPath());
182+
return testingDirectory;
183+
} catch (IOException e) {
184+
throw new IllegalArgumentException("Failed to copy directory", e);
185+
}
186+
}
187+
160188
/**
161189
* Saves the fixture to the appropriate location.
162190
*
@@ -193,6 +221,25 @@ private static String streamToText(InputStream stream) {
193221
return "";
194222
}
195223

224+
public static void copyFolder(Path source, Path target, CopyOption... options) throws IOException {
225+
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
226+
227+
@Override
228+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
229+
throws IOException {
230+
Files.createDirectories(target.resolve(source.relativize(dir)));
231+
return FileVisitResult.CONTINUE;
232+
}
233+
234+
@Override
235+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
236+
throws IOException {
237+
Files.copy(file, target.resolve(source.relativize(file)), options);
238+
return FileVisitResult.CONTINUE;
239+
}
240+
});
241+
}
242+
196243
private InputStream readStreamFromClasspath(String fileName) {
197244
return clazz.getResourceAsStream(clazz.getSimpleName() + '/' + fileName);
198245
}

libs/fixt/src/test/groovy/com/agorapulse/testing/fixt/FixtSpec.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,16 @@ class FixtSpec extends Specification {
6767
new File(contextTestFolder, TEXT_FILE).text == TEXT_CONTENT
6868
}
6969

70+
void 'copy directory'() {
71+
given:
72+
Fixt fixt = Fixt.create(this)
73+
File destinationDir = tmp.newFolder()
74+
when:
75+
fixt.copyTo('testdir', destinationDir) // <8>
76+
then:
77+
new File(destinationDir, 'root.txt').exists() // <9>
78+
new File(destinationDir, 'firstLevel/firstLevel.txt').exists()
79+
new File(destinationDir, 'firstLevel/secondLevel/secondLevel.txt').exists()
80+
}
81+
7082
}

libs/fixt/src/test/resources/com/agorapulse/testing/fixt/FixtSpec/testdir/firstLevel/firstLevel.txt

Whitespace-only changes.

libs/fixt/src/test/resources/com/agorapulse/testing/fixt/FixtSpec/testdir/firstLevel/secondLevel/secondLevel.txt

Whitespace-only changes.

libs/fixt/src/test/resources/com/agorapulse/testing/fixt/FixtSpec/testdir/root.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)