Skip to content

Commit 8a86894

Browse files
committed
test with strict mode enabled and fixed
1 parent f30167e commit 8a86894

File tree

2 files changed

+65
-30
lines changed

2 files changed

+65
-30
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ Execute the test suite with Gradlew:
9797
gradlew clean build test
9898
```
9999

100+
*Optional* Execute the test suite in strict mode with Gradlew:
101+
102+
```shell
103+
gradlew testWithStrictMode
104+
```
105+
100106
# Notes
101107

102108
For more information, please see [NOTES.md](https://github.com/stleary/JSON-java/blob/master/docs/NOTES.md)

build.gradle

+59-30
Original file line numberDiff line numberDiff line change
@@ -53,46 +53,75 @@ publishing {
5353
tasks.withType(JavaCompile) {
5454
options.encoding = 'UTF-8'
5555
}
56+
// Add these imports at the top of your build.gradle file
57+
import java.nio.file.Files
58+
import java.nio.file.Path
59+
import java.nio.file.Paths
60+
import java.nio.file.StandardCopyOption
5661

57-
def originalFile = null;
62+
// Your existing build configurations...
5863

59-
task backupCode {
60-
def file = file('src/main/java/org/json/JSONParserConfiguration.java')
61-
originalFile = file.text
62-
}
64+
// Add a new task to modify the file
65+
task modifyStrictMode {
66+
doLast {
67+
println "Modifying JSONParserConfiguration.java to enable strictMode..."
6368

64-
task firstTest {
69+
def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java')
6570

66-
}
71+
if (!filePath.exists()) {
72+
throw new GradleException("Could not find file: ${filePath.absolutePath}")
73+
}
6774

68-
task modifyCode {
69-
doLast {
70-
// Add your code modification logic here
71-
def file = file('src/main/java/org/json/JSONParserConfiguration.java')
72-
def text = file.text
73-
text = text.replaceAll('oldCode', 'newCode')
74-
file.text = text
75+
// Create a backup of the original file
76+
def backupFile = new File(filePath.absolutePath + '.bak')
77+
Files.copy(filePath.toPath(), backupFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
78+
79+
// Read and modify the file content
80+
def content = filePath.text
81+
def modifiedContent = content.replace('// this.strictMode = true;', 'this.strictMode = true;')
82+
83+
// Write the modified content back to the file
84+
filePath.text = modifiedContent
85+
86+
println "File modified successfully at: ${filePath.absolutePath}"
7587
}
7688
}
7789

78-
task compileModifiedCode(type: JavaCompile) {
79-
source = sourceSets.main.java.srcDirs
80-
classpath = sourceSets.main.compileClasspath
81-
destinationDirectory = sourceSets.main.java.outputDir
82-
}
90+
// Add a task to restore the original file
91+
task restoreStrictMode {
92+
doLast {
93+
println "Restoring original JSONParserConfiguration.java..."
8394

84-
task secondTest {
95+
def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java')
96+
def backupFile = new File(filePath.absolutePath + '.bak')
8597

98+
if (backupFile.exists()) {
99+
Files.copy(backupFile.toPath(), filePath.toPath(), StandardCopyOption.REPLACE_EXISTING)
100+
backupFile.delete()
101+
println "Original file restored successfully at: ${filePath.absolutePath}"
102+
} else {
103+
println "Backup file not found at: ${backupFile.absolutePath}. No restoration performed."
104+
}
105+
}
86106
}
87107

88-
task restoreCode {
89-
def file = file('src/main/java/org/json/JSONParserConfiguration.java')
90-
file.text = originalFile
91-
}
108+
// Create a task to run the workflow
109+
task testWithStrictMode {
110+
dependsOn modifyStrictMode
111+
finalizedBy restoreStrictMode
92112

93-
// and then add it to the task list
94-
backupCode.finalizedBy firstTest
95-
firstTest.finalizedBy modifyCode
96-
modifyCode.finalizedBy compileModifiedCode
97-
compileModifiedCode.finalizedBy secondTest
98-
secondTest.finalizedBy restoreCode
113+
doLast {
114+
// This will trigger a clean build and run tests with strictMode enabled
115+
if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
116+
exec {
117+
executable 'cmd'
118+
args '/c', 'gradlew.bat', 'clean', 'build'
119+
}
120+
} else {
121+
exec {
122+
executable './gradlew'
123+
args 'clean', 'build'
124+
}
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)