Skip to content

Commit

Permalink
Replace container creationTime and filesModificationTime getters/sett…
Browse files Browse the repository at this point in the history
…ers with Property (#3708)

* Removes String getters/setters
* Adds getter for Property instead
  • Loading branch information
creckord committed Sep 21, 2022
1 parent de65c0d commit 56a4ae6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;

Expand All @@ -52,14 +51,14 @@ public class ContainerParameters {
private String appRoot = "";
@Nullable private String user;
@Nullable private String workingDirectory;
private Property<String> filesModificationTime;
private Property<String> creationTime;
private final Property<String> filesModificationTime;
private final Property<String> creationTime;

@Inject
public ContainerParameters(ObjectFactory objectFactory) {
labels = objectFactory.mapProperty(String.class, String.class).empty();
filesModificationTime = objectFactory.property(String.class).value("EPOCH_PLUS_SECOND");
creationTime = objectFactory.property(String.class).value("EPOCH");
filesModificationTime = objectFactory.property(String.class).convention("EPOCH_PLUS_SECOND");
creationTime = objectFactory.property(String.class).convention("EPOCH");
}

@Input
Expand Down Expand Up @@ -266,35 +265,21 @@ public void setWorkingDirectory(String workingDirectory) {

@Input
@Optional
public String getFilesModificationTime() {
if (System.getProperty(PropertyNames.CONTAINER_FILES_MODIFICATION_TIME) != null) {
return System.getProperty(PropertyNames.CONTAINER_FILES_MODIFICATION_TIME);
public Property<String> getFilesModificationTime() {
String property = System.getProperty(PropertyNames.CONTAINER_FILES_MODIFICATION_TIME);
if (property != null && !property.equals(filesModificationTime.get())) {
filesModificationTime.set(property);
}
return filesModificationTime.getOrNull();
}

public void setFilesModificationTime(Provider<String> filesModificationTime) {
this.filesModificationTime.set(filesModificationTime);
}

public void setFilesModificationTime(String filesModificationTime) {
this.filesModificationTime.set(filesModificationTime);
return filesModificationTime;
}

@Input
@Optional
public String getCreationTime() {
if (System.getProperty(PropertyNames.CONTAINER_CREATION_TIME) != null) {
return System.getProperty(PropertyNames.CONTAINER_CREATION_TIME);
public Property<String> getCreationTime() {
String property = System.getProperty(PropertyNames.CONTAINER_CREATION_TIME);
if (property != null && !property.equals(creationTime.get())) {
creationTime.set(property);
}
return creationTime.getOrNull();
}

public void setCreationTime(Provider<String> creationTime) {
this.creationTime.set(creationTime);
}

public void setCreationTime(String creationTime) {
this.creationTime.set(creationTime);
return creationTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ public Optional<String> getProperty(String propertyName) {

@Override
public String getFilesModificationTime() {
return jibExtension.getContainer().getFilesModificationTime();
return jibExtension.getContainer().getFilesModificationTime().get();
}

@Override
public String getCreationTime() {
return jibExtension.getContainer().getCreationTime();
return jibExtension.getContainer().getCreationTime().get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashSet;
import java.util.Optional;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -51,6 +52,8 @@ public void testGetters() {
OutputPathsParameters outputPathsParameters = Mockito.mock(OutputPathsParameters.class);
CredHelperParameters fromCredHelperParameters = Mockito.mock(CredHelperParameters.class);
CredHelperParameters toCredHelperParameters = Mockito.mock(CredHelperParameters.class);
Property<String> filesModificationTime = Mockito.mock(Property.class);
Property<String> creationTime = Mockito.mock(Property.class);

Mockito.when(authParameters.getUsername()).thenReturn("user");
Mockito.when(authParameters.getPassword()).thenReturn("password");
Expand Down Expand Up @@ -90,7 +93,10 @@ public void testGetters() {
Mockito.when(containerParameters.getMainClass()).thenReturn("com.example.Main");
Mockito.when(containerParameters.getPorts()).thenReturn(Arrays.asList("80/tcp", "0"));
Mockito.when(containerParameters.getUser()).thenReturn("admin:wheel");
Mockito.when(containerParameters.getFilesModificationTime()).thenReturn("2011-12-03T22:42:05Z");
Mockito.when(containerParameters.getFilesModificationTime()).thenReturn(filesModificationTime);
Mockito.when(filesModificationTime.get()).thenReturn("2011-12-03T22:42:05Z");
Mockito.when(containerParameters.getCreationTime()).thenReturn(creationTime);
Mockito.when(creationTime.get()).thenReturn("2011-12-03T11:42:05Z");

Mockito.when(dockerClientParameters.getExecutablePath()).thenReturn(Paths.get("test"));
Mockito.when(dockerClientParameters.getEnvironment())
Expand Down Expand Up @@ -135,6 +141,7 @@ public void testGetters() {
rawConfiguration.getToCredHelper().getEnvironment());
Assert.assertEquals("admin:wheel", rawConfiguration.getUser().get());
Assert.assertEquals("2011-12-03T22:42:05Z", rawConfiguration.getFilesModificationTime());
Assert.assertEquals("2011-12-03T11:42:05Z", rawConfiguration.getCreationTime());
Assert.assertEquals(Paths.get("test"), rawConfiguration.getDockerExecutable().get());
Assert.assertEquals(
new HashMap<>(ImmutableMap.of("docker", "client")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ public void testContainer() {
assertThat(testJibExtension.getContainer().getPorts()).isEmpty();
assertThat(testJibExtension.getContainer().getLabels().get()).isEmpty();
assertThat(testJibExtension.getContainer().getAppRoot()).isEmpty();
assertThat(testJibExtension.getContainer().getFilesModificationTime())
assertThat(testJibExtension.getContainer().getFilesModificationTime().get())
.isEqualTo("EPOCH_PLUS_SECOND");
assertThat(testJibExtension.getContainer().getCreationTime()).isEqualTo("EPOCH");
assertThat(testJibExtension.getContainer().getCreationTime().get()).isEqualTo("EPOCH");

testJibExtension.container(
container -> {
Expand All @@ -200,7 +200,7 @@ public void testContainer() {
container.setPorts(Arrays.asList("1000", "2000-2010", "3000"));
container.setFormat(ImageFormat.OCI);
container.setAppRoot("some invalid appRoot value");
container.setFilesModificationTime("some invalid time value");
container.getFilesModificationTime().set("some invalid time value");
});
ContainerParameters container = testJibExtension.getContainer();
assertThat(container.getEntrypoint()).containsExactly("foo", "bar", "baz").inOrder();
Expand All @@ -215,7 +215,7 @@ public void testContainer() {
assertThat(container.getPorts()).containsExactly("1000", "2000-2010", "3000").inOrder();
assertThat(container.getFormat()).isSameInstanceAs(ImageFormat.OCI);
assertThat(container.getAppRoot()).isEqualTo("some invalid appRoot value");
assertThat(container.getFilesModificationTime()).isEqualTo("some invalid time value");
assertThat(container.getFilesModificationTime().get()).isEqualTo("some invalid time value");
}

@Test
Expand Down Expand Up @@ -482,8 +482,11 @@ public void testProperties() {
System.setProperty("jib.container.user", "myUser");
assertThat(testJibExtension.getContainer().getUser()).isEqualTo("myUser");
System.setProperty("jib.container.filesModificationTime", "2011-12-03T22:42:05Z");
assertThat(testJibExtension.getContainer().getFilesModificationTime())
assertThat(testJibExtension.getContainer().getFilesModificationTime().get())
.isEqualTo("2011-12-03T22:42:05Z");
System.setProperty("jib.container.creationTime", "2011-12-03T11:42:05Z");
assertThat(testJibExtension.getContainer().getCreationTime().get())
.isEqualTo("2011-12-03T11:42:05Z");
System.setProperty("jib.containerizingMode", "packaged");
assertThat(testJibExtension.getContainerizingMode()).isEqualTo("packaged");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ tasks.register('check-extra-directories') {
}

tasks.register('showtimes') {
String prop = project.extensions.getByName('jib')['container']['creationTime']
String prop = project.extensions.jib.container.creationTime.get()
println('creationTime=' + prop)
prop = project.extensions.getByName('jib')['container']['filesModificationTime']
prop = project.extensions.jib.container.filesModificationTime.get()
println('filesModificationTime=' + prop)
}

0 comments on commit 56a4ae6

Please sign in to comment.