This repository has been archived by the owner on May 25, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sca): add normal npm decl support
- Loading branch information
Showing
5 changed files
with
74 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
analyser_sca/src/main/kotlin/org/archguard/analyser/sca/parser/NpmParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.archguard.analyser.sca.parser | ||
|
||
import com.jayway.jsonpath.JsonPath | ||
import org.archguard.analyser.sca.model.DeclFileTree | ||
import org.archguard.analyser.sca.model.DepDeclaration | ||
import org.archguard.analyser.sca.model.DepDependency | ||
|
||
class NpmParser : Parser() { | ||
override fun lookupSource(file: DeclFileTree): List<DepDeclaration> { | ||
val name: String = JsonPath.read(file.content, "$.name") | ||
val version: String = JsonPath.read(file.content, "$.version") | ||
val depMap: Map<String, String> = JsonPath.read(file.content, "$.dependencies") | ||
|
||
return listOf(DepDeclaration( | ||
name, | ||
version, | ||
"npm", | ||
this.createDependencies(depMap) | ||
)) | ||
} | ||
|
||
private fun createDependencies(depMap: Map<String, String>): List<DepDependency> { | ||
return depMap.map { | ||
DepDependency( | ||
name = it.key, | ||
group = "", | ||
artifact = it.key, | ||
version = it.value | ||
) | ||
}.toList() | ||
} | ||
} |
8 changes: 0 additions & 8 deletions
8
analyser_sca/src/main/kotlin/org/archguard/analyser/sca/parser/ParserUtil.kt
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
analyser_sca/src/test/kotlin/org/archguard/analyser/sca/JavaScriptFinderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.archguard.analyser.sca | ||
|
||
import org.junit.jupiter.api.Test | ||
|
||
internal class JavaScriptFinderTest { | ||
@Test | ||
internal fun should_handle_json() { | ||
|
||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
analyser_sca/src/test/kotlin/org/archguard/analyser/sca/parser/NpmParserTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.archguard.analyser.sca.parser | ||
|
||
import org.archguard.analyser.sca.model.DeclFileTree | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
|
||
internal class NpmParserTest { | ||
private val samplePackageJson = """{ | ||
"name": "my_package", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"my_dep": "^1.0.0", | ||
"another_dep": "~2.2.0" | ||
} | ||
} | ||
""".trimIndent() | ||
|
||
@Test | ||
fun first_dep() { | ||
val declFileTree = DeclFileTree("package.json", "package.json", samplePackageJson) | ||
val declTree = NpmParser().lookupSource(declFileTree) | ||
|
||
assertEquals(1, declTree.size) | ||
assertEquals("my_package", declTree[0].name) | ||
assertEquals("1.0.0", declTree[0].version) | ||
assertEquals(2, declTree[0].dependencies.size) | ||
} | ||
} |