<commit_before>package org.xtext.gradle.test

import com.google.common.base.Charsets
import com.google.common.collect.ImmutableMap
import com.google.common.hash.HashCode
import com.google.common.hash.Hashing
import com.google.common.io.Files
import java.io.File
import java.util.Map
import java.util.Set
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.Data
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.gradle.tooling.GradleConnectionException
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.ProjectConnection
import static org.junit.Assert.*
import org.junit.rules.ExternalResource
import org.junit.rules.TemporaryFolder

@FinalFieldsConstructor
class ProjectUnderTest extends ExternalResource {
	val temp = new TemporaryFolder
	@Accessors val String projectName
	ProjectConnection gradle

	new() {
		this("root")
	}
	
	override protected before() throws Throwable {
		temp.create
		temp.newFolder(projectName)
		gradle = GradleConnector.newConnector
			.forProjectDirectory(rootDir)
			.connect
	}
	
	override protected after() {
		gradle.close
		temp.delete
	}
	
	def File getRootDir() {
		new File(temp.root, projectName)
	}
	
	def void setBuildFile(CharSequence content) {
		new File(rootDir, 'build.gradle').content = content
	}
	
	def File file(String relativePath) {
		new File(rootDir, relativePath)
	}
	
	def File createFile(String relativePath, CharSequence content) {
		val file = file(relativePath)
		file.content = content
		file
	}
	
	def void setContent(File file, CharSequence content) {
		file.parentFile.mkdirs
		file.createNewFile
		Files.write(content, file, Charsets.UTF_8)
	}
	
	def String getContentAsString(File file) {
		Files.toString(file, Charsets.UTF_8)
	}
	
	def void shouldExist(File file) {
		assertTrue(file.exists)
	}
	
	def void shouldContain(File file, CharSequence content) {
		assertEquals(content.toString, file.contentAsString)
	}
	
	def FileCollectionSnapshot snapshot() {
		FileCollectionSnapshot.forFolder(file("build"))
	}
	
	def BuildResult executeTasks(String... tasks) {
		val result = new BuildResult
		try {
			gradle.newBuild.setStandardError(System.err).setStandardOutput(System.out).forTasks(tasks).run
		} catch (GradleConnectionException e) {
			result.failure = e
		}
		result
	}
	
	@Accessors(PUBLIC_GETTER)
	static class BuildResult {
		Throwable failure<commit_msg>capture standard out and err from daemon<commit_after>package org.xtext.gradle.test

import com.google.common.base.Charsets
import com.google.common.collect.ImmutableMap
import com.google.common.hash.HashCode
import com.google.common.hash.Hashing
import com.google.common.io.Files
import java.io.File
import java.util.Map
import java.util.Set
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.Data
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.gradle.tooling.GradleConnectionException
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.ProjectConnection
import static org.junit.Assert.*
import org.junit.rules.ExternalResource
import org.junit.rules.TemporaryFolder
import java.io.ByteArrayOutputStream

@FinalFieldsConstructor
class ProjectUnderTest extends ExternalResource {
	val temp = new TemporaryFolder
	@Accessors val String projectName
	ProjectConnection gradle

	new() {
		this("root")
	}
	
	override protected before() throws Throwable {
		temp.create
		temp.newFolder(projectName)
		gradle = GradleConnector.newConnector
			.forProjectDirectory(rootDir)
			.connect
	}
	
	override protected after() {
		gradle.close
		temp.delete
	}
	
	def File getRootDir() {
		new File(temp.root, projectName)
	}
	
	def void setBuildFile(CharSequence content) {
		new File(rootDir, 'build.gradle').content = content
	}
	
	def File file(String relativePath) {
		new File(rootDir, relativePath)
	}
	
	def File createFile(String relativePath, CharSequence content) {
		val file = file(relativePath)
		file.content = content
		file
	}
	
	def void setContent(File file, CharSequence content) {
		file.parentFile.mkdirs
		file.createNewFile
		Files.write(content, file, Charsets.UTF_8)
	}
	
	def String getContentAsString(File file) {
		Files.toString(file, Charsets.UTF_8)
	}
	
	def void shouldExist(File file) {
		assertTrue(file.exists)
	}
	
	def void shouldContain(File file, CharSequence content) {
		assertEquals(content.toString, file.contentAsString)
	}
	
	def FileCollectionSnapshot snapshot() {
		FileCollectionSnapshot.forFolder(file("build"))
	}
	
	def BuildResult executeTasks(String... tasks) {
		val result = new BuildResult
		try {
			val out = new ByteArrayOutputStream
			val err = new ByteArrayOutputStream
			gradle.newBuild
				.setStandardOutput(out)
				.setStandardError(err)
				.forTasks(tasks)
				.run
			result.standardOut = new String(out.toByteArray, Charsets.UTF_8)
			result.standardErr = new String(err.toByteArray, Charsets.UTF_8)
		} catch (GradleConnectionException e) {
			result.failure = e
		}
		result
	}
	
	@Accessors(PUBLIC_GETTER)
	static class BuildResult {
		Throwable failure
		String standardOut
		String standardErr
{'fraction_non_alphanumeric': 0.087984496124031, 'fraction_numerical': 0.0011627906976744186, 'mean_word_length': 4.715393133997785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6016234', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1559, 'n_words': 349}