test with strict mode enabled and fixed

This commit is contained in:
marilynel 2025-03-02 11:02:27 -08:00
parent f30167e7c0
commit 8a86894c63
2 changed files with 69 additions and 34 deletions

View File

@ -97,6 +97,12 @@ Execute the test suite with Gradlew:
gradlew clean build test gradlew clean build test
``` ```
*Optional* Execute the test suite in strict mode with Gradlew:
```shell
gradlew testWithStrictMode
```
# Notes # Notes
For more information, please see [NOTES.md](https://github.com/stleary/JSON-java/blob/master/docs/NOTES.md) For more information, please see [NOTES.md](https://github.com/stleary/JSON-java/blob/master/docs/NOTES.md)

View File

@ -53,46 +53,75 @@ publishing {
tasks.withType(JavaCompile) { tasks.withType(JavaCompile) {
options.encoding = 'UTF-8' options.encoding = 'UTF-8'
} }
// Add these imports at the top of your build.gradle file
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
def originalFile = null; // Your existing build configurations...
task backupCode { // Add a new task to modify the file
def file = file('src/main/java/org/json/JSONParserConfiguration.java') task modifyStrictMode {
originalFile = file.text
}
task firstTest {
}
task modifyCode {
doLast { doLast {
// Add your code modification logic here println "Modifying JSONParserConfiguration.java to enable strictMode..."
def file = file('src/main/java/org/json/JSONParserConfiguration.java')
def text = file.text def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java')
text = text.replaceAll('oldCode', 'newCode')
file.text = text if (!filePath.exists()) {
throw new GradleException("Could not find file: ${filePath.absolutePath}")
}
// Create a backup of the original file
def backupFile = new File(filePath.absolutePath + '.bak')
Files.copy(filePath.toPath(), backupFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
// Read and modify the file content
def content = filePath.text
def modifiedContent = content.replace('// this.strictMode = true;', 'this.strictMode = true;')
// Write the modified content back to the file
filePath.text = modifiedContent
println "File modified successfully at: ${filePath.absolutePath}"
} }
} }
task compileModifiedCode(type: JavaCompile) { // Add a task to restore the original file
source = sourceSets.main.java.srcDirs task restoreStrictMode {
classpath = sourceSets.main.compileClasspath doLast {
destinationDirectory = sourceSets.main.java.outputDir println "Restoring original JSONParserConfiguration.java..."
def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java')
def backupFile = new File(filePath.absolutePath + '.bak')
if (backupFile.exists()) {
Files.copy(backupFile.toPath(), filePath.toPath(), StandardCopyOption.REPLACE_EXISTING)
backupFile.delete()
println "Original file restored successfully at: ${filePath.absolutePath}"
} else {
println "Backup file not found at: ${backupFile.absolutePath}. No restoration performed."
}
}
} }
task secondTest { // Create a task to run the workflow
task testWithStrictMode {
dependsOn modifyStrictMode
finalizedBy restoreStrictMode
doLast {
// This will trigger a clean build and run tests with strictMode enabled
if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
exec {
executable 'cmd'
args '/c', 'gradlew.bat', 'clean', 'build'
}
} else {
exec {
executable './gradlew'
args 'clean', 'build'
}
}
}
} }
task restoreCode {
def file = file('src/main/java/org/json/JSONParserConfiguration.java')
file.text = originalFile
}
// and then add it to the task list
backupCode.finalizedBy firstTest
firstTest.finalizedBy modifyCode
modifyCode.finalizedBy compileModifiedCode
compileModifiedCode.finalizedBy secondTest
secondTest.finalizedBy restoreCode