From ee779a98ca45959a41c6e1c614e4f90b19783a3f Mon Sep 17 00:00:00 2001 From: Lennart Heimbs Date: Tue, 12 Dec 2023 09:09:01 +0100 Subject: [PATCH] add GradleTaskExecutor unit tests Signed-off-by: Lennart Heimbs --- .../execution/GradleTaskExecutorTest.kt | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 pitmutationmate/src/test/kotlin/com/amos/pitmutationmate/pitmutationmate/execution/GradleTaskExecutorTest.kt diff --git a/pitmutationmate/src/test/kotlin/com/amos/pitmutationmate/pitmutationmate/execution/GradleTaskExecutorTest.kt b/pitmutationmate/src/test/kotlin/com/amos/pitmutationmate/pitmutationmate/execution/GradleTaskExecutorTest.kt new file mode 100644 index 00000000..9116b87a --- /dev/null +++ b/pitmutationmate/src/test/kotlin/com/amos/pitmutationmate/pitmutationmate/execution/GradleTaskExecutorTest.kt @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: MIT +// SPDX-FileCopyrightText: 2023 Lennart Heimbs + +package com.amos.pitmutationmate.pitmutationmate.execution + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations + +/** + * Tests for GradleTaskExecutor + * + * @see GradleTaskExecutor + */ +class GradleTaskExecutorTest { + @Mock + lateinit var systemInfo: SystemInfoProvider // Assuming SystemInfo is a dependency + + @InjectMocks + lateinit var gradleTaskExecutor: GradleTaskExecutor + + @BeforeEach + fun setup() { + MockitoAnnotations.openMocks(this) + } + + @Test + fun `test buildCommandLine for Windows`() { + `when`(systemInfo.isWindows()).thenReturn(true) + + val commandLine = gradleTaskExecutor.buildCommandLine( + null, + "clean", + "/path/to/project", + "com.example.Class", + 8080 + ) + + assertEquals(GradleTaskExecutor.WINDOWS_SHELL_EXECUTABLE, commandLine.exePath) + assertEquals(GradleTaskExecutor.WINDOWS_FIRST_PARAMETER, commandLine.parametersList.parameters[0]) + assertEquals(GradleTaskExecutor.WINDOWS_GRADLE_EXECUTABLE, commandLine.parametersList.parameters[1]) + } + + @Test + fun `test buildCommandLine for Unix`() { + `when`(systemInfo.isWindows()).thenReturn(false) + + val commandLine = gradleTaskExecutor.buildCommandLine( + null, + "clean", + "/path/to/project", + "com.example.Class", + 8080 + ) + + assertEquals(GradleTaskExecutor.UNIX_SHELL_EXECUTABLE, commandLine.exePath) + assertEquals(GradleTaskExecutor.UNIX_FIRST_PARAMETER, commandLine.parametersList.parameters[0]) + assertEquals(GradleTaskExecutor.UNIX_GRADLE_EXECUTABLE, commandLine.parametersList.parameters[1]) + } + + @Test + fun `test buildCommandLine without taskName uses default taskName`() { + `when`(systemInfo.isWindows()).thenReturn(true) + + val commandLine = gradleTaskExecutor.buildCommandLine( + null, + null, + "/path/to/project", + "com.example.Class", + 8080 + ) + + assertEquals(GradleTaskExecutor.PITEST_TASK_NAME, commandLine.parametersList.parameters[2]) + } + + @Test + fun `test buildCommandLine with taskName uses given taskName`() { + `when`(systemInfo.isWindows()).thenReturn(true) + + val taskName = "test123" + val commandLine = gradleTaskExecutor.buildCommandLine( + null, + taskName, + "/path/to/project", + "com.example.Class", + 8080 + ) + + assertEquals(taskName, commandLine.parametersList.parameters[2]) + } + + @Test + fun `test buildCommandLine without classFQDN does not add targetClass override`() { + `when`(systemInfo.isWindows()).thenReturn(true) + + val commandLine = gradleTaskExecutor.buildCommandLine( + null, + "clean", + "/path/to/project", + null, + 8080 + ) + + for (parameter in commandLine.parametersList.parameters) { + assertFalse(parameter.contains("-Dpitmutationmate.override.targetClasses")) + } + } + + @Test + fun `test buildCommandLine with classFQDN adds targetClass override`() { + `when`(systemInfo.isWindows()).thenReturn(true) + + val classFQN = "com.example.Class" + val commandLine = gradleTaskExecutor.buildCommandLine( + null, + "clean", + "/path/to/project", + classFQN, + 8080 + ) + + assertTrue( + commandLine.parametersList.parameters.contains("-Dpitmutationmate.override.targetClasses=$classFQN") + ) + } +}