|
| 1 | +/* |
| 2 | + * Copyright 2025 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.cli.core; |
| 17 | + |
| 18 | +import java.nio.file.Path; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.mockito.Mockito; |
| 22 | + |
| 23 | +import com.diffplug.spotless.ResourceHarness; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +class ExecutionLayoutTest extends ResourceHarness { |
| 28 | + |
| 29 | + @Test |
| 30 | + void itResolvesGradleBuildDir() { |
| 31 | + setFile("settings.gradle").toLines("rootProject.name = 'test'"); |
| 32 | + ExecutionLayout layout = ExecutionLayout.create(fileResolver(), commandLineStream()); |
| 33 | + |
| 34 | + Path buildDir = layout.buildDir(); |
| 35 | + |
| 36 | + assertThat(buildDir.toString()) |
| 37 | + .matches("\\Q%s\\E.*\\Q%s\\E.*" |
| 38 | + .formatted( |
| 39 | + rootFolder().toPath().toString(), |
| 40 | + Path.of("build", "spotless-cli").toString())); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void itResolvesMavenBuildDir() { |
| 45 | + setFile("pom.xml").toLines("<project><modelVersion>4.0.0</modelVersion></project>"); |
| 46 | + ExecutionLayout layout = ExecutionLayout.create(fileResolver(), commandLineStream()); |
| 47 | + |
| 48 | + Path buildDir = layout.buildDir(); |
| 49 | + |
| 50 | + assertThat(buildDir.toString()) |
| 51 | + .matches("\\Q%s\\E.*\\Q%s\\E.*" |
| 52 | + .formatted( |
| 53 | + rootFolder().toPath().toString(), |
| 54 | + Path.of("target", "spotless-cli").toString())); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void itResolvesTmpBuildDir() { |
| 59 | + ExecutionLayout layout = ExecutionLayout.create(fileResolver(), commandLineStream()); |
| 60 | + |
| 61 | + Path buildDir = layout.buildDir(); |
| 62 | + |
| 63 | + assertThat(buildDir.toString()) |
| 64 | + .doesNotStartWith(rootFolder().toPath().toString()) |
| 65 | + .startsWith(System.getProperty("java.io.tmpdir")); |
| 66 | + } |
| 67 | + |
| 68 | + private FileResolver fileResolver() { |
| 69 | + return new FileResolver(rootFolder().toPath()); |
| 70 | + } |
| 71 | + |
| 72 | + private SpotlessCommandLineStream commandLineStream() { |
| 73 | + return Mockito.mock(SpotlessCommandLineStream.class); |
| 74 | + } |
| 75 | +} |
0 commit comments