From 454ec28792769d3827e0de723c4f4fc9b13fe249 Mon Sep 17 00:00:00 2001 From: ambhrin-slalom <89942600+ambhrin-slalom@users.noreply.github.com> Date: Fri, 26 Apr 2024 13:24:23 -0500 Subject: [PATCH] feat: tests for language detection (#532) secureli-XXX ## Changes * ## Testing * ## Clean Code Checklist - [ ] Meets acceptance criteria for issue - [ ] New logic is covered with automated tests - [ ] Appropriate exception handling added - [ ] Thoughtful logging included - [ ] Documentation is updated - [ ] Follow-up work is documented in TODOs - [ ] TODOs have a ticket associated with them - [ ] No commented-out code included --------- Co-authored-by: isaac-heist-slalom --- .secureli/.pre-commit-config.yaml | 36 ++++++------ pyproject.toml | 1 + secureli/main.py | 2 +- secureli/modules/shared/models/repository.py | 2 +- .../test-data/Csharp_Sample/src/Program.cs | 9 +++ .../test-data/Go_Sample/src/main.go | 7 +++ .../JavaScript_Sample/public/index.html | 11 ++++ .../test-data/JavaScript_Sample/src/index.js | 1 + .../test-data/Kotlin_Sample/src/Main.kt | 1 + .../test-data/Python_Sample/main.py | 1 + .../test-data/Swift_Sample/Sources/main.swift | 1 + .../test-data/Terraform_Sample/main.tf | 1 + .../test-data/TypeScript_Sample/src/index.ts | 1 + tests/end-to-end/test-language-detect.bats | 56 +++++++++++++++++++ 14 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 tests/end-to-end/test-data/Csharp_Sample/src/Program.cs create mode 100644 tests/end-to-end/test-data/Go_Sample/src/main.go create mode 100644 tests/end-to-end/test-data/JavaScript_Sample/public/index.html create mode 100644 tests/end-to-end/test-data/JavaScript_Sample/src/index.js create mode 100644 tests/end-to-end/test-data/Kotlin_Sample/src/Main.kt create mode 100644 tests/end-to-end/test-data/Python_Sample/main.py create mode 100644 tests/end-to-end/test-data/Swift_Sample/Sources/main.swift create mode 100644 tests/end-to-end/test-data/Terraform_Sample/main.tf create mode 100644 tests/end-to-end/test-data/TypeScript_Sample/src/index.ts create mode 100644 tests/end-to-end/test-language-detect.bats diff --git a/.secureli/.pre-commit-config.yaml b/.secureli/.pre-commit-config.yaml index 37767dff..25af6f94 100644 --- a/.secureli/.pre-commit-config.yaml +++ b/.secureli/.pre-commit-config.yaml @@ -1,18 +1,18 @@ -# See https://pre-commit.com for more information -# See https://pre-commit.com/hooks.html for more hooks -repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.6.0 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: check-added-large-files -- repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.4.0 - hooks: - - id: black -- repo: https://github.com/yelp/detect-secrets - rev: v1.4.0 - hooks: - - id: detect-secrets +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files +- repo: https://github.com/psf/black-pre-commit-mirror + rev: 24.4.1 + hooks: + - id: black +- repo: https://github.com/yelp/detect-secrets + rev: v1.4.0 + hooks: + - id: detect-secrets diff --git a/pyproject.toml b/pyproject.toml index 5f8b3d6c..1301fdc5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ lint = "black --check ." precommit = "pre-commit run --config .secureli/.pre-commit-config.yaml --all-files" test = ["init", "lint", "coverage_run", "coverage_report"] e2e = "bats --verbose-run tests/end-to-end" +lang-test = "bats --verbose-run tests/end-to-end/test-language-detect.bats" [tool.poetry.dependencies] # Until `python-dependency-injector` supports python 3.12, restrict to python 3.11 and lower diff --git a/secureli/main.py b/secureli/main.py index 6cf8ff37..07b24e62 100644 --- a/secureli/main.py +++ b/secureli/main.py @@ -37,7 +37,7 @@ def version_callback(value: bool): config = pre_commit_abstr.get_pre_commit_config(path) all_repos = [ - (hook.id, repo.rev.lstrip("v")) + (hook.id, repo.rev.lstrip("v") if repo.rev else None) for repo in config.repos for hook in repo.hooks ] diff --git a/secureli/modules/shared/models/repository.py b/secureli/modules/shared/models/repository.py index e116ccc9..2615f13c 100644 --- a/secureli/modules/shared/models/repository.py +++ b/secureli/modules/shared/models/repository.py @@ -49,7 +49,7 @@ class PreCommitRepo(BaseModel): """ url: str = Field(alias="repo") - rev: str + rev: Optional[str] hooks: list[PreCommitHook] = Field(default=[]) suppressed_hook_ids: list[str] = Field(default=[]) diff --git a/tests/end-to-end/test-data/Csharp_Sample/src/Program.cs b/tests/end-to-end/test-data/Csharp_Sample/src/Program.cs new file mode 100644 index 00000000..6e1130f5 --- /dev/null +++ b/tests/end-to-end/test-data/Csharp_Sample/src/Program.cs @@ -0,0 +1,9 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, world!"); + } +} diff --git a/tests/end-to-end/test-data/Go_Sample/src/main.go b/tests/end-to-end/test-data/Go_Sample/src/main.go new file mode 100644 index 00000000..ef25884d --- /dev/null +++ b/tests/end-to-end/test-data/Go_Sample/src/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello, world!") +} diff --git a/tests/end-to-end/test-data/JavaScript_Sample/public/index.html b/tests/end-to-end/test-data/JavaScript_Sample/public/index.html new file mode 100644 index 00000000..080cb0eb --- /dev/null +++ b/tests/end-to-end/test-data/JavaScript_Sample/public/index.html @@ -0,0 +1,11 @@ + + + + + + JavaScript_Sample + + + + + diff --git a/tests/end-to-end/test-data/JavaScript_Sample/src/index.js b/tests/end-to-end/test-data/JavaScript_Sample/src/index.js new file mode 100644 index 00000000..a8141d3b --- /dev/null +++ b/tests/end-to-end/test-data/JavaScript_Sample/src/index.js @@ -0,0 +1 @@ +console.log("Hello, world!"); diff --git a/tests/end-to-end/test-data/Kotlin_Sample/src/Main.kt b/tests/end-to-end/test-data/Kotlin_Sample/src/Main.kt new file mode 100644 index 00000000..d113eced --- /dev/null +++ b/tests/end-to-end/test-data/Kotlin_Sample/src/Main.kt @@ -0,0 +1 @@ +fun main() { println("Hello, world!") } diff --git a/tests/end-to-end/test-data/Python_Sample/main.py b/tests/end-to-end/test-data/Python_Sample/main.py new file mode 100644 index 00000000..f7cf60e1 --- /dev/null +++ b/tests/end-to-end/test-data/Python_Sample/main.py @@ -0,0 +1 @@ +print("Hello, world!") diff --git a/tests/end-to-end/test-data/Swift_Sample/Sources/main.swift b/tests/end-to-end/test-data/Swift_Sample/Sources/main.swift new file mode 100644 index 00000000..f7cf60e1 --- /dev/null +++ b/tests/end-to-end/test-data/Swift_Sample/Sources/main.swift @@ -0,0 +1 @@ +print("Hello, world!") diff --git a/tests/end-to-end/test-data/Terraform_Sample/main.tf b/tests/end-to-end/test-data/Terraform_Sample/main.tf new file mode 100644 index 00000000..69b152ca --- /dev/null +++ b/tests/end-to-end/test-data/Terraform_Sample/main.tf @@ -0,0 +1 @@ +output "hello_world" { value = "Hello, world!" } diff --git a/tests/end-to-end/test-data/TypeScript_Sample/src/index.ts b/tests/end-to-end/test-data/TypeScript_Sample/src/index.ts new file mode 100644 index 00000000..a8141d3b --- /dev/null +++ b/tests/end-to-end/test-data/TypeScript_Sample/src/index.ts @@ -0,0 +1 @@ +console.log("Hello, world!"); diff --git a/tests/end-to-end/test-language-detect.bats b/tests/end-to-end/test-language-detect.bats new file mode 100644 index 00000000..afaad83d --- /dev/null +++ b/tests/end-to-end/test-language-detect.bats @@ -0,0 +1,56 @@ +setup() { + load "${BATS_LIBS_ROOT}/bats-support/load" + load "${BATS_LIBS_ROOT}/bats-assert/load" +} + +# Tests currently commented out as they have a dependency on test data which will be workied on in a future ticket. In order to run them currently, you would need to go into +# each one of the language repo folder under test-data folder and git initialize them. In the future we wish to automate the creation and git initialization of each one of these +# folders + +#@test "can detect C# language" { +# run python secureli/main.py init -ryd tests/end-to-end/test-data/Csharp_Sample/ +# assert_output --partial '[seCureLI] The following language(s) support secrets detection: C#' +# assert_output --partial '[seCureLI] - C#: 100%' +#} + +#@test "can detect Go language" { +# run python secureli/main.py init -ryd tests/end-to-end/test-data/Go_Sample/ +# assert_output --partial '[seCureLI] - Go: 100%' +# assert_output --partial '[seCureLI] seCureLI has been installed successfully for the following language(s): Go.' +#} + +#@test "can detect Javascript language" { +# run python secureli/main.py init -ryd tests/end-to-end/test-data/JavaScript_Sample/ +# assert_output --partial '[seCureLI] - JavaScript: 100%' +# assert_output --partial '[seCureLI] seCureLI has been installed successfully for the following language(s): JavaScript.' +#} + +#@test "can detect Kotlin language" { +# run python secureli/main.py init -ryd tests/end-to-end/test-data/Kotlin_Sample/ +# assert_output --partial '[seCureLI] - Kotlin: 100%' +# assert_output --partial '[seCureLI] seCureLI has been installed successfully for the following language(s): Kotlin.' +#} + +#@test "can detect Python language" { +# run python secureli/main.py init -ryd tests/end-to-end/test-data/Python_Sample/ +# assert_output --partial '[seCureLI] - Python: 100%' +# assert_output --partial '[seCureLI] seCureLI has been installed successfully for the following language(s): Python.' +#} + +#@test "can detect Swift language" { +# run python secureli/main.py init -ryd tests/end-to-end/test-data/Swift_Sample/ +# assert_output --partial '[seCureLI] seCureLI has been installed successfully for the following language(s): Swift.' +# assert_output --partial '[seCureLI] - Swift: 100%' +#} + +#@test "can detect Terraform language" { +# run python secureli/main.py init -ryd tests/end-to-end/test-data/Terraform_Sample/ +# assert_output --partial '[seCureLI] - Terraform: 100%' +# assert_output --partial '[seCureLI] seCureLI has been installed successfully for the following language(s): Terraform.' +#} + +#@test "can detect Typescript language" { +# run python secureli/main.py init -ryd tests/end-to-end/test-data/Typescript_Sample/ +# assert_output --partial '[seCureLI] - TypeScript: 100%' +# assert_output --partial '[seCureLI] seCureLI has been installed successfully for the following language(s): TypeScript.' +#}