Skip to content

Commit

Permalink
Sets up initial test module structure, container test rule and initia…
Browse files Browse the repository at this point in the history
…l Skaffold file tests. (GoogleContainerTools#40)

* Set up initial test module structure, container test rule, dependencies and Skaffold tests.

* Implemented junit4 rule.
  • Loading branch information
ivanporty authored Oct 3, 2018
1 parent 0e618f4 commit afe55d3
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 1 deletion.
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ allprojects {
kotlinOptions.jvmTarget = "1.8"
}

dependencies {
testCompile("com.google.truth:truth:+") {
exclude(group = "com.google.guava", module = "guava")
}
}

spotless {
kotlin {
target("**/src/**/*.kt")
Expand Down
15 changes: 15 additions & 0 deletions common-test-lib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed 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.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed 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 com.google.container.tools.test

import com.intellij.testFramework.EdtTestUtil
import com.intellij.testFramework.fixtures.IdeaProjectTestFixture
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory
import com.intellij.util.ThrowableRunnable
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

/**
* A custom [TestRule] for Container Tools unit tests.
*
* This rule adds the following functionality:
*
* * Creates an [IdeaProjectTestFixture] and makes it available for tests via property. By default
* this text fixture is "light", i.e. instance of [LightIdeaTestFixture].
*/
class ContainerToolsRule : TestRule {
lateinit var ideaProjectTestFixture: IdeaProjectTestFixture

override fun apply(baseStatement: Statement, description: Description): Statement =
object : Statement() {
override fun evaluate() {
try {
setUpRule()
baseStatement.evaluate()
} finally {
tearDownRule()
}
}
}

private fun setUpRule() {
ideaProjectTestFixture =
IdeaTestFixtureFactory.getFixtureFactory().createLightFixtureBuilder().fixture
EdtTestUtil.runInEdtAndWait(ThrowableRunnable { ideaProjectTestFixture.setUp() })
}

private fun tearDownRule() {
EdtTestUtil.runInEdtAndWait(ThrowableRunnable { ideaProjectTestFixture.tearDown() })
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
*/

include 'skaffold'
include 'common-test-lib'
19 changes: 19 additions & 0 deletions skaffold/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed 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.
*/

dependencies {
compile(project(":common-test-lib"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private val SKAFFOLD_API_HEADER_PATTERN: Pattern by lazy {
/**
* Checks if a given file is a valid Skaffold configuration file based on type and API version.
*/
internal fun isSkaffoldFile(file: VirtualFile): Boolean {
fun isSkaffoldFile(file: VirtualFile): Boolean {
with(file) {
if (!isDirectory && fileType is YAMLFileType && isValid) {
val inputStream: InputStream = ByteArrayInputStream(contentsToByteArray())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed 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 com.google.container.tools.skaffold

import com.google.common.truth.Truth.assertThat
import com.google.container.tools.test.ContainerToolsRule
import com.intellij.mock.MockVirtualFile
import org.junit.Rule
import org.junit.Test

/** Unit tests for Skaffold files functionality in [SkaffoldFiles.kt] */
class SkaffoldFilesTest {
@get:Rule
val containerToolsRule = ContainerToolsRule()

@Test
fun `valid skaffold file is accepted`() {
val skaffoldFile = MockVirtualFile.file("skaffold.yaml")
skaffoldFile.setText("apiVersion: skaffold/v1alpha2")

assertThat(isSkaffoldFile(skaffoldFile)).isTrue()
}

@Test
fun `extra spaces and tabs in header skaffold file is accepted`() {
val skaffoldFile = MockVirtualFile.file("skaffold.yaml")
skaffoldFile.setText(
""" apiVersion: skaffold/v1alpha3
kind: Config
build:
"""
)

assertThat(isSkaffoldFile(skaffoldFile)).isTrue()
}

@Test
fun `kubernetes file is not valid skaffold file`() {
val k8sFile = MockVirtualFile.file("deploy.yaml")
k8sFile.setText("apiVersion: apps/v1")

assertThat(isSkaffoldFile(k8sFile)).isFalse()
}
}

0 comments on commit afe55d3

Please sign in to comment.