|
| 1 | +/* |
| 2 | + * Copyright 2020 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.gradle.spotless; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.attribute.PosixFilePermissions; |
| 22 | + |
| 23 | +import org.assertj.core.api.AbstractStringAssert; |
| 24 | +import org.assertj.core.api.Assertions; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import com.diffplug.spotless.FileSignature; |
| 28 | + |
| 29 | +public class FilePermissionsTest extends GradleIntegrationHarness { |
| 30 | + @Test |
| 31 | + public void spotlessApplyShouldPreservePermissions() throws IOException { |
| 32 | + if (FileSignature.machineIsWin()) { |
| 33 | + // need unix filesystem |
| 34 | + return; |
| 35 | + } |
| 36 | + setFile("build.gradle").toLines( |
| 37 | + "buildscript { repositories { mavenCentral() } }", |
| 38 | + "plugins {", |
| 39 | + " id 'com.diffplug.spotless'", |
| 40 | + "}", |
| 41 | + "", |
| 42 | + "spotless {", |
| 43 | + " java {", |
| 44 | + " target file('test.java')", |
| 45 | + " googleJavaFormat('1.2')", |
| 46 | + " }", |
| 47 | + "}"); |
| 48 | + setFile("test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test"); |
| 49 | + |
| 50 | + Path path = rootFolder().toPath().resolve("test.java"); |
| 51 | + Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxr--r--")); |
| 52 | + assertPermissions(path).isEqualTo("rwxr--r--"); |
| 53 | + |
| 54 | + gradleRunner().withArguments("spotlessApply").build(); |
| 55 | + assertFile("test.java").sameAsResource("java/googlejavaformat/JavaCodeFormatted.test"); |
| 56 | + assertPermissions(path).isEqualTo("rwxr--r--"); |
| 57 | + } |
| 58 | + |
| 59 | + private AbstractStringAssert<?> assertPermissions(Path path) throws IOException { |
| 60 | + return Assertions.assertThat(PosixFilePermissions.toString(Files.getPosixFilePermissions(path))); |
| 61 | + } |
| 62 | +} |
0 commit comments