Skip to content
This repository has been archived by the owner on Mar 19, 2019. It is now read-only.

Implement findAllIn for YmlType so you can actually use it #252

Merged
merged 1 commit into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Retain all changes from an editor
https://github.com/atomist/rug/issues/199

- Yml type can now be instantiated from ProjectMutableView,
DirectoryMutableView, and FileMutableView,
https://github.com/atomist/rug/issues/250

### Changed

- **BREAKING** `TreeNode.nodeType` renamed to `TreeNode.nodeTags`.
Expand Down
31 changes: 23 additions & 8 deletions src/main/scala/com/atomist/rug/kind/yml/YmlType.scala
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
package com.atomist.rug.kind.yml

import com.atomist.project.ProjectOperationArguments
import com.atomist.rug.kind.core.{DirectoryMutableView, FileMutableView, ProjectMutableView}
import com.atomist.rug.kind.dynamic.ContextlessViewFinder
import com.atomist.rug.parser.Selected
import com.atomist.rug.runtime.rugdsl.{DefaultEvaluator, Evaluator}
import com.atomist.rug.spi.{MutableView, ReflectivelyTypedType, Type}
import com.atomist.source.ArtifactSource
import com.atomist.tree.TreeNode

object YmlType {
val ymlExtension = ".yml"
}

class YmlType(
evaluator: Evaluator
)
extends Type(evaluator)
with ContextlessViewFinder
with ReflectivelyTypedType {

import YmlType._

def this() = this(DefaultEvaluator)

override def description = "YML file"

override def viewManifest: Manifest[YmlMutableView] = manifest[YmlMutableView]

// override protected def findInternal(typeName: String, creationInfo: Option[Object], rugAs: ArtifactSource, projectSource: ArtifactSource, poa: ProjectOperationArguments, identifierMap: Map[String, Object], targetAlias: String): Seq[YmlMutableView] = {
// val pmv = new ProjectMutableView(rugAs, projectSource)
// projectSource.allFiles
// .filter(f => f.name.endsWith(".yml"))
// .map(f => new YmlMutableView(f, new ProjectMutableView(rugAs, projectSource)))
// .toSeq
// }
override protected def findAllIn(rugAs: ArtifactSource,
selected: Selected,
context: TreeNode,
poa: ProjectOperationArguments,
identifierMap: Map[String, Object]): Option[Seq[MutableView[_]]] = ???
identifierMap: Map[String, Object]): Option[Seq[MutableView[_]]] = {
context match {
case pmv: ProjectMutableView =>
Some(pmv.originalBackingObject.allFiles
.filter(f => f.name.endsWith(ymlExtension))
.map(f => new YmlMutableView(f, pmv)))
case dmv: DirectoryMutableView =>
Some(dmv.originalBackingObject.allFiles
.filter(f => f.name.endsWith(ymlExtension))
.map(f => new YmlMutableView(f, dmv.parent)))
case fmv: FileMutableView =>
Some(Seq(fmv.originalBackingObject)
.filter(f => f.name.endsWith(ymlExtension))
.map(f => new YmlMutableView(f, fmv.parent)))
}
}

/**
* The set of node types this can resolve from
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/atomist/rug/kind/pom/PomUsageTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PomUsageTest extends FlatSpec with Matchers with LazyLogging {
|editor PomEdit
|
|with Pom x when path = "pom.xml"
|do groupId
| do groupId
""".stripMargin

updateWith(prog, JavaTypeUsageTest.NewSpringBootProject) match {
Expand Down
219 changes: 219 additions & 0 deletions src/test/scala/com/atomist/rug/kind/yml/YmlUsageTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package com.atomist.rug.kind.yml

import com.atomist.project.SimpleProjectOperationArguments
import com.atomist.project.edit.{NoModificationNeeded, SuccessfulModification}
import com.atomist.rug.DefaultRugPipeline
import com.atomist.rug.InterpreterRugPipeline.DefaultRugArchive
import com.atomist.source.{ArtifactSource, EmptyArtifactSource, SimpleFileBasedArtifactSource, StringFileArtifact}
import org.scalatest.{FlatSpec, Matchers}

class YmlUsageTest extends FlatSpec with Matchers {
import com.atomist.rug.TestUtils._

val xYml =
"""
|group: queen
|artifact: "A Night at the Opera"
|dependencies:
|- "Death on Two Legs (Dedicated to....)"
|- "Lazing on a Sunday Afternoon"
|- "I'm in Love with My Car"
|- "You're My Best Friend"
|- "'39"
|- "Sweet Lady"
|- "Seaside Rendezvous"
|- "The Prophet's Song"
|- "Love of My Life"
|- "Good Company"
|- "Bohemian Rhapsody"
|- "God Save the Queen"
|common: everywhere
""".stripMargin

val yYml =
"""
|artist: Paul Simon
|album: Graceland
|songs:
|- The Boy in the Bubble
|- Graceland
|- I Know What I Know
|- Gumboots
|- Diamonds on the Soles of Her Shoes
|- You Can Call Me Al
|- Under African Skies
|- Homeless
|- Crazy Love, Vol. II
|- That Was Your Mother
|- All Around the World or the Myth of Fingerprints
|common: everywhere
""".stripMargin

val zYml =
"""
|something: completely different
|this:
| structure:
| is:
| - more
| - nested
|that:
|- is
|- not
|some:
| nesting: one level
|common: everywhere
""".stripMargin

val singleAS = new SimpleFileBasedArtifactSource("single",
Seq(StringFileArtifact("x.yml", xYml))
)

val yamlAS = new SimpleFileBasedArtifactSource("triple",
Seq(
StringFileArtifact("x.yml", xYml),
StringFileArtifact("src/main/y.yml", yYml),
StringFileArtifact("target/build/z.yml", zYml)
)
)

val fullAS = new SimpleFileBasedArtifactSource("full",
Seq(
StringFileArtifact("README.md", "This is a README\n"),
StringFileArtifact("x.yml", xYml),
StringFileArtifact("src/main/y.yml", yYml),
StringFileArtifact("src/main/not-yml.txt", "Not YAML file.\n\nWe should not find this.\n"),
StringFileArtifact("target/build/z.yml", zYml)
)
)

val allAS = Seq((singleAS, 1), (yamlAS, 3), (fullAS, 3))

private def runProgAndCheck(prog: String, as: ArtifactSource, mods: Int): ArtifactSource = {
val progArtifact: ArtifactSource = new SimpleFileBasedArtifactSource(DefaultRugArchive,
StringFileArtifact(new DefaultRugPipeline().defaultFilenameFor(prog), prog)
)

val modAttempt = attemptModification(progArtifact, as, EmptyArtifactSource(""),
SimpleProjectOperationArguments("", Map.empty[String,Object]))

modAttempt match {
case sm: SuccessfulModification if sm.result.cachedDeltas.size == mods =>
sm.result
case _: NoModificationNeeded if mods == 0 =>
as
case ma =>
fail(s"incorrect number of changes: $mods; $prog; $as; $ma")
as
}
}

it should "get group value with no change with native Rug function" in {
val prog =
"""
|editor YmlEdit
|
|with Yml x when path = "x.yml"
| do valueOf "group"
""".stripMargin
allAS.foreach(asChanges => runProgAndCheck(prog, asChanges._1, 0))
}

it should "change group value with native Rug function" in {
val prog =
"""
|editor YmlEdit
|
|with Yml x when path = "x.yml"
| do updateKey "group" "Marx Brothers"
""".stripMargin
allAS.foreach(asChanges => runProgAndCheck(prog, asChanges._1, 1))
}

it should "change group value only if exists with native Rug function" in {
val prog =
"""
|editor YmlEdit
|
|with Yml x
| do updateKey "group" "Marx Brothers"
""".stripMargin
allAS.foreach(asChanges => runProgAndCheck(prog, asChanges._1, 1))
}

it should "change group value in all files with native Rug function" in {
val prog =
"""
|editor YmlEdit
|
|with Yml x
| do updateKey "common" "Be"
""".stripMargin
allAS.foreach(asChanges => runProgAndCheck(prog, asChanges._1, asChanges._2))
}

it should "get group value via tree expression with no change with native Rug function" in {
val prog =
"""
|editor YmlEdit
|
|let pe = $(/*[@name='x.yml']/Yml())
|
|with pe
| do valueOf "group"
""".stripMargin
allAS.foreach(asChanges => runProgAndCheck(prog, asChanges._1, 0))
}

it should "change group value via tree expression with native Rug function" in {
val prog =
"""
|editor YmlEdit
|
|let pe = $(/*[@name='x.yml']/Yml())
|
|with pe
| do updateKey "group" "Marx Brothers"
""".stripMargin
allAS.foreach(asChanges => runProgAndCheck(prog, asChanges._1, 1))
}

it should "change group value via tree expression only if exists with native Rug function" in {
val prog =
"""
|editor YmlEdit
|
|let pe = $(/Yml())
|
|with pe
| do updateKey "group" "Marx Brothers"
""".stripMargin
allAS.foreach(asChanges => runProgAndCheck(prog, asChanges._1, 1))
}

it should "change group value via tree expression in all files with native Rug function" in {
val prog =
"""
|editor YmlEdit
|
|let pe = $(/Yml())
|
|with pe
| do updateKey "common" "Be"
""".stripMargin
allAS.foreach(asChanges => runProgAndCheck(prog, asChanges._1, asChanges._2))
}

it should "make no changes when tree expression does not match any files" in {
val prog =
"""
|editor YmlEdit
|
|let pe = $(/src/*[@name='x.yml']/Yml())
|
|with pe
| do updateKey "common" "Be"
""".stripMargin
allAS.foreach(asChanges => runProgAndCheck(prog, asChanges._1, 0))
}
}