|
| 1 | +// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.gradle |
| 5 | + |
| 6 | +import org.assertj.core.api.Assertions.assertThat |
| 7 | +import org.eclipse.jgit.api.Git |
| 8 | +import org.eclipse.jgit.storage.file.FileRepositoryBuilder |
| 9 | +import org.gradle.testfixtures.ProjectBuilder |
| 10 | +import org.gradle.testkit.runner.GradleRunner |
| 11 | +import org.gradle.testkit.runner.TaskOutcome |
| 12 | +import org.gradle.testkit.runner.UnexpectedBuildFailure |
| 13 | +import org.junit.jupiter.api.Test |
| 14 | +import org.junit.jupiter.api.assertThrows |
| 15 | +import org.junit.jupiter.api.io.TempDir |
| 16 | +import java.io.File |
| 17 | +import kotlin.io.path.writeText |
| 18 | + |
| 19 | +class GitSecretsTest { |
| 20 | + @Test |
| 21 | + fun `plugin can be applied`() { |
| 22 | + val project = ProjectBuilder.builder().build() |
| 23 | + project.getPluginManager().apply("toolkit-git-secrets") |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + fun `passes when no secrets`(@TempDir tempDir: File) { |
| 28 | + tempDir.mkdirs() |
| 29 | + val repo = FileRepositoryBuilder() |
| 30 | + .setWorkTree(tempDir) |
| 31 | + .build() |
| 32 | + repo.create() |
| 33 | + |
| 34 | + tempDir |
| 35 | + .resolve("build.gradle.kts") |
| 36 | + .writeText( |
| 37 | + """ |
| 38 | + plugins { |
| 39 | + id("toolkit-git-secrets") |
| 40 | + } |
| 41 | + """.trimIndent() |
| 42 | + ) |
| 43 | + |
| 44 | + Git.wrap(repo).add().addFilepattern(".").call() |
| 45 | + |
| 46 | + val result = GradleRunner.create() |
| 47 | + .withProjectDir(tempDir) |
| 48 | + .withArguments("gitSecrets") |
| 49 | + .withPluginClasspath() |
| 50 | + .build() |
| 51 | + |
| 52 | + assertThat(result.task(":gitSecrets")?.outcome).isEqualTo(TaskOutcome.SUCCESS) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `fails when contains secrets`(@TempDir tempDir: File) { |
| 57 | + tempDir.mkdirs() |
| 58 | + val repo = FileRepositoryBuilder() |
| 59 | + .setWorkTree(tempDir) |
| 60 | + .build() |
| 61 | + repo.create() |
| 62 | + |
| 63 | + tempDir |
| 64 | + .resolve("build.gradle.kts") |
| 65 | + .apply { |
| 66 | + writeText( |
| 67 | + """ |
| 68 | + plugins { |
| 69 | + id("toolkit-git-secrets") |
| 70 | + } |
| 71 | + """.trimIndent() |
| 72 | + ) |
| 73 | + |
| 74 | + appendText( |
| 75 | + buildString { |
| 76 | + appendLine() |
| 77 | + // split to avoid tripping git-secrets |
| 78 | + append("// AKI") |
| 79 | + append("AXXXXXXXXXXXXXXXX") |
| 80 | + } |
| 81 | + ) |
| 82 | + |
| 83 | + Git.wrap(repo).add().addFilepattern(".").call() |
| 84 | + } |
| 85 | + |
| 86 | + val failure = assertThrows<UnexpectedBuildFailure> { |
| 87 | + GradleRunner.create() |
| 88 | + .withProjectDir(tempDir) |
| 89 | + .withArguments("gitSecrets") |
| 90 | + .withPluginClasspath() |
| 91 | + .build() |
| 92 | + } |
| 93 | + assertThat(failure.message).contains("Matched one or more prohibited patterns") |
| 94 | + } |
| 95 | +} |
0 commit comments