Skip to content

Commit e13f768

Browse files
fs: copy file/dir
1 parent b2c3d65 commit e13f768

6 files changed

Lines changed: 118 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
message inside file
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package scenarios.fs
2+
3+
import static org.testingisdocumenting.webtau.WebTauGroovyDsl.*
4+
5+
scenario('copy file') {
6+
def dir = fs.tempDir('fs-copy')
7+
fs.copy('data/message.txt', dir)
8+
fs.textContent(dir.resolve('message.txt')).should == 'message inside file'
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2021 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.featuretesting
18+
19+
import org.junit.BeforeClass
20+
import org.junit.Test
21+
22+
class WebTauFileSystemFeaturesTest {
23+
private static WebTauEndToEndTestRunner testRunner
24+
25+
@BeforeClass
26+
static void init() {
27+
testRunner = new WebTauEndToEndTestRunner()
28+
}
29+
30+
@Test
31+
void "copy"() {
32+
runCli('copy.groovy', 'webtau.cfg.groovy')
33+
}
34+
35+
private static void runCli(String testName, String configFileName, String... additionalArgs) {
36+
testRunner.runCli("scenarios/fs/$testName",
37+
configFileName.isEmpty() ? "" : "scenarios/fs/$configFileName", additionalArgs)
38+
}
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"scenarioDetails" : [ {
3+
"scenario" : "copy file",
4+
"shortContainerId" : "copy.groovy",
5+
"stepsSummary" : {
6+
"numberOfSuccessful" : 3
7+
}
8+
} ],
9+
"exitCode" : 0
10+
}

webtau-filesystem/src/main/java/org/testingisdocumenting/webtau/fs/zip/UnzipTask.java renamed to webtau-filesystem/src/main/java/org/testingisdocumenting/webtau/ant/UnzipTask.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2021 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,7 +15,7 @@
1415
* limitations under the License.
1516
*/
1617

17-
package org.testingisdocumenting.webtau.fs.zip;
18+
package org.testingisdocumenting.webtau.ant;
1819

1920
import org.apache.ant.compress.taskdefs.Unzip;
2021
import org.apache.tools.ant.Project;

webtau-filesystem/src/main/java/org/testingisdocumenting/webtau/fs/FileSystem.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.testingisdocumenting.webtau.fs;
1818

1919
import org.apache.commons.io.FileUtils;
20-
import org.testingisdocumenting.webtau.fs.zip.UnzipTask;
20+
import org.testingisdocumenting.webtau.ant.UnzipTask;
2121
import org.testingisdocumenting.webtau.reporter.StepReportOptions;
2222
import org.testingisdocumenting.webtau.reporter.WebTauStep;
2323

@@ -58,6 +58,28 @@ public void unzip(String src, String dest) {
5858
unzip(Paths.get(src), Paths.get(dest));
5959
}
6060

61+
public void copy(String src, Path dest) {
62+
copy(Paths.get(src), dest);
63+
}
64+
65+
public void copy(String src, String dest) {
66+
copy(Paths.get(src), Paths.get(dest));
67+
}
68+
69+
public void copy(Path src, Path dest) {
70+
WebTauStep step = WebTauStep.createStep(null,
71+
tokenizedMessage(action("copying"), urlValue(src.toString()), TO, urlValue(dest.toString())),
72+
(Object r) -> {
73+
CopyResult result = (CopyResult) r;
74+
return tokenizedMessage(action("copied"), classifier(result.type),
75+
urlValue(result.fullSrc.toAbsolutePath().toString()), TO,
76+
urlValue(result.fullDest.toAbsolutePath().toString()));
77+
},
78+
() -> copyImpl(src, dest));
79+
80+
step.execute(StepReportOptions.REPORT_ALL);
81+
}
82+
6183
public FileTextContent textContent(Path path) {
6284
return new FileTextContent(path);
6385
}
@@ -110,6 +132,28 @@ public Path fullPath(Path relativeOrFull) {
110132
return getCfg().getWorkingDir().resolve(relativeOrFull).toAbsolutePath();
111133
}
112134

135+
private static CopyResult copyImpl(Path src, Path dest) {
136+
Path fullSrc = fs.fullPath(src);
137+
Path fullDest = fs.fullPath(dest);
138+
139+
try {
140+
if (Files.isDirectory(fullSrc) && Files.isDirectory(fullDest)) {
141+
FileUtils.copyDirectory(fullSrc.toFile(), fullDest.toFile());
142+
return new CopyResult("directory", fullSrc, fullDest);
143+
} else {
144+
Path dstForFile = Files.isDirectory(fullDest) ?
145+
fullDest.resolve(fullSrc.getFileName()) :
146+
fullDest;
147+
148+
FileUtils.copyFile(fullSrc.toFile(), dstForFile.toFile());
149+
150+
return new CopyResult("file", fullSrc, dstForFile);
151+
}
152+
} catch (IOException e) {
153+
throw new UncheckedIOException(e);
154+
}
155+
}
156+
113157
private static Path createTempDir(Path dir, String prefix) {
114158
try {
115159
if (dir != null) {
@@ -126,4 +170,16 @@ private static Path createTempDir(Path dir, String prefix) {
126170
throw new UncheckedIOException(e);
127171
}
128172
}
173+
174+
static class CopyResult {
175+
private final String type;
176+
private final Path fullSrc;
177+
private final Path fullDest;
178+
179+
public CopyResult(String type, Path fullSrc, Path fullDest) {
180+
this.type = type;
181+
this.fullSrc = fullSrc;
182+
this.fullDest = fullDest;
183+
}
184+
}
129185
}

0 commit comments

Comments
 (0)