Skip to content

Commit df0e54a

Browse files
committed
Merge pull request #324 from brunobowden/testing
projectClearDir
2 parents 4983412 + ce2d96f commit df0e54a

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/TranslateTask.groovy

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ class TranslateTask extends DefaultTask {
113113
// if the user requests it with the UNSAFE_incrementalBuildClosure argument.
114114
// TODO: One correct way to incrementally compile with --build-closure would be to use
115115
// allInputFiles someway, but this will require some research.
116-
if (srcGenDir.exists()) {
117-
srcGenDir.deleteDir()
118-
srcGenDir.mkdirs()
119-
}
116+
Utils.projectClearDir(project, srcGenDir)
120117
srcFilesChanged = originalSrcFiles
121118
} else {
122119
boolean nonSourceFileChanged = false
@@ -194,10 +191,7 @@ class TranslateTask extends DefaultTask {
194191
// A change outside of the source set directories has occurred, so an incremental build isn't possible.
195192
// The most common such change is in the JAR for a dependent library, for example if Java project
196193
// that this project depends on had its source changed and was recompiled.
197-
if (srcGenDir.exists()) {
198-
srcGenDir.deleteDir()
199-
srcGenDir.mkdirs()
200-
}
194+
Utils.projectClearDir(project, srcGenDir)
201195
srcFilesChanged = originalSrcFiles
202196
}
203197
}

src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/Utils.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,24 @@ class Utils {
382382
return proj.delete(paths)
383383
}
384384

385+
/**
386+
* Delete a directory and recreate it using project.delete(...) and project.mkdir(...)
387+
*
388+
* Must be called instead of project.delete(...) to allow mocking of project calls in testing.
389+
* May fail in the case where the parent directory doesn't exist. This is because it uses
390+
* Project.mkdir(...) instead of File.mkdirs(...). Note that if the parameter is an
391+
* @OutputDirectory, then the directory is automatically created before the task runs.
392+
*
393+
* @param proj Calls proj.delete(...) method and then project.mkdir(...)
394+
* @param paths Variable length list of paths to be deleted, can be String or File
395+
*/
396+
// See projectExec for explanation of the code
397+
@CompileStatic(TypeCheckingMode.SKIP)
398+
static boolean projectClearDir(Project proj, File path) {
399+
proj.delete(path)
400+
proj.mkdir(path)
401+
}
402+
385403
/**
386404
* Executes command line and returns result by calling project.exec(...)
387405
*

src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/MockProjectExec.groovy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class MockProjectExec {
128128
}
129129

130130
void verify() {
131-
mockForProj.verify(projectProxyInstance())
131+
mockForProj.verify((GroovyObject) projectProxyInstance())
132132
}
133133

134134
void demandCopyAndReturn(String intoParam, String... fromParam) {
@@ -209,6 +209,16 @@ class MockProjectExec {
209209
}
210210
}
211211

212+
void demandDeleteThenMkDirAndReturn(String expectedPath) {
213+
214+
mockForProj.demand.delete { Object path ->
215+
assert expectedPath == getAbsolutePath(path)
216+
}
217+
mockForProj.demand.mkdir { Object path ->
218+
assert expectedPath == getAbsolutePath(path)
219+
}
220+
}
221+
212222
void demandExecAndReturn(List<String> expectedCommandLine) {
213223
demandExecAndReturn(null, expectedCommandLine, null, null, null)
214224
}

0 commit comments

Comments
 (0)