Skip to content

Commit

Permalink
refactor: convert FileContentsTask to Java
Browse files Browse the repository at this point in the history
Relates to elastic#34459
  • Loading branch information
baptistemesta committed Oct 23, 2018
1 parent 0b4e8db commit 9d73661
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 61 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ class VagrantTestPlugin implements Plugin<Project> {

Task createLinuxRunnerScript = project.tasks.create('createLinuxRunnerScript', FileContentsTask) {
dependsOn copyPackagingTests
file "${testsDir}/run-tests.sh"
contents """\
file = "${testsDir}/run-tests.sh"
contents = """\
if [ "\$#" -eq 0 ]; then
test_args=( "${-> project.extensions.esvagrant.testClass}" )
else
Expand All @@ -257,10 +257,10 @@ class VagrantTestPlugin implements Plugin<Project> {
}
Task createWindowsRunnerScript = project.tasks.create('createWindowsRunnerScript', FileContentsTask) {
dependsOn copyPackagingTests
file "${testsDir}/run-tests.ps1"
file = "${testsDir}/run-tests.ps1"
// the use of $args rather than param() here is deliberate because the syntax for array (multivalued) parameters is likely
// a little trappy for those unfamiliar with powershell
contents """\
contents = """\
if (\$args.Count -eq 0) {
\$testArgs = @("${-> project.extensions.esvagrant.testClass}")
} else {
Expand All @@ -273,14 +273,14 @@ class VagrantTestPlugin implements Plugin<Project> {

Task createVersionFile = project.tasks.create('createVersionFile', FileContentsTask) {
dependsOn copyPackagingArchives
file "${archivesDir}/version"
contents project.version
file = "${archivesDir}/version"
contents = project.version
}

Task createUpgradeFromFile = project.tasks.create('createUpgradeFromFile', FileContentsTask) {
dependsOn copyPackagingArchives
file "${archivesDir}/upgrade_from_version"
contents project.extensions.esvagrant.upgradeFromVersion.toString()
file = "${archivesDir}/upgrade_from_version"
contents = project.extensions.esvagrant.upgradeFromVersion.toString()
}

Task createUpgradeIsOssFile = project.tasks.create('createUpgradeIsOssFile', FileContentsTask) {
Expand All @@ -291,8 +291,8 @@ class VagrantTestPlugin implements Plugin<Project> {
throw new StopExecutionException("upgrade version is before 6.3.0")
}
}
file "${archivesDir}/upgrade_is_oss"
contents ''
file = "${archivesDir}/upgrade_is_oss"
contents = ''
}

File batsDir = new File(packagingDir, BATS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.gradle;

import static java.nio.file.Files.write;

import java.io.File;
import java.io.IOException;

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

/**
* Creates a file and sets it contents to something.
*/
public class FileContentsTask extends DefaultTask {

/**
* The file to be built.
*/
@OutputFile
private File file;

@Input
private String contents;


/**
* the contents to be written in the file
*/
public void setContents(String contents) {
this.contents = contents;
}

/**
* The file to be built. Takes a String or a File and coerces it to a file.
*/
public void setFile(File file) {
this.file = file;
}

public void setFile(String file) {
this.file = getProject().file(file);
}

@TaskAction
public void writeFile() throws IOException {
write(file.toPath(), contents.getBytes("UTF-8"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.elasticsearch.gradle;


import static java.util.Collections.singletonMap;
import static org.apache.commons.io.FileUtils.readFileToString;

import java.io.File;
import java.io.IOException;

import org.elasticsearch.gradle.test.GradleUnitTestCase;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;

public class FileContentsTaskTests extends GradleUnitTestCase {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private FileContentsTask fileContentsTask;
private Project project;

public void testWriteFile() throws Exception {
project = createProject();
fileContentsTask = createTask(project);
File targetFile = new File(project.getProjectDir(),"testFile");
String expectedContent = "the expected content\nis here.";

fileContentsTask.setFile(targetFile);
fileContentsTask.setContents(expectedContent);
fileContentsTask.writeFile();

assertEquals(expectedContent, readFileToString(targetFile));
}

public void testWriteFileSpecifiedByString() throws Exception {
project = createProject();
fileContentsTask = createTask(project);
File targetFile = new File(project.getProjectDir(),"testFile");
String expectedContent = "the expected content\nis here.";

fileContentsTask.setFile("testFile");
fileContentsTask.setContents(expectedContent);
fileContentsTask.writeFile();

assertEquals(expectedContent, readFileToString(targetFile));
}

private Project createProject() throws IOException {
return ProjectBuilder.builder().withProjectDir(temporaryFolder.newFolder()).build();
}

private FileContentsTask createTask(Project project) {
return (FileContentsTask) project.task(singletonMap("type", FileContentsTask.class), "fileContentTask");
}

}

0 comments on commit 9d73661

Please sign in to comment.