Skip to content

Instantly share code, notes, and snippets.

/ruby.rb Secret

Created June 8, 2015 00:43
Show Gist options
  • Save anonymous/2f4308c33ea377c45c38 to your computer and use it in GitHub Desktop.
Save anonymous/2f4308c33ea377c45c38 to your computer and use it in GitHub Desktop.
require 'rexml/document'
def write_version(path, version)
pom_dir = File.dirname(path)
pom_xml = REXML::Document.new(File.read(path))
REXML::XPath.first(pom_xml, '/project/version').text = version.dup
write_file(path, pom_xml)
modules = REXML::XPath.each(pom_xml, '/project/modules/module').map(&:text)
modules.each do |m|
pom_path = File.join(pom_dir, m, 'pom.xml')
write_child_version(pom_path, version, modules)
end
end
def write_child_version(path, version, modules)
pom_xml = REXML::Document.new(File.read(path))
REXML::XPath.first(pom_xml, '/project/parent/version').text = version.dup
REXML::XPath.each(pom_xml, '/project/dependencies/dependency').select {|el| modules.include?(el.elements['artifactId'].text)}.each do |el|
element = el.elements['version']
next if element.nil?
element.text = version.dup
end
write_file(path, pom_xml)
end
def write_file(path, contents)
puts contents
File.open(path, 'w+') { |f| f.write(contents) }
system("git add #{path}")
end
def git_commit(branch)
system("git commit -m \"[job-creator] prepare for next development iteration\"")
system("git push origin #{branch}")
end
def git_tag(tag_name, branch)
system("git commit -m \"[job-creator] prepare release #{tag_name}\"")
system("git tag #{tag_name}")
system("git push origin --tags #{branch}:#{branch}")
end
--------------------------------
describe "bump version" do
context "pom.xml is valid" do
pom_xml_pre = """
<?xml version='1.0' encoding='UTF-8'?>
<project xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd' xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<modelVersion>4.0.0</modelVersion>
<artifactId>core-service-parent</artifactId>
<version>1.3.2-SNAPSHOT</version>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<description>Core Service Parent POM</description>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>core-service-test</module>
</modules>
</profile>
</profiles>
</project>
"""
pom_xml_post = """
<?xml version='1.0' encoding='UTF-8'?>
<project xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd' xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<modelVersion>4.0.0</modelVersion>
<artifactId>core-service-parent</artifactId>
<version>1.4.0</version>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<description>Core Service Parent POM</description>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>core-service-test</module>
</modules>
</profile>
</profiles>
</project>
"""
it "successfully writes new version" do
file = double('file')
content = double('content') { pom_xml_post }
File.should_receive(:read).once.with('pom.xml').and_return(pom_xml_pre)
File.should_receive(:open).once.with('pom.xml', 'w+').and_yield(file)
file.should_receive(:write).once.with('blah')
write_version('pom.xml', '1.4.0')
end
context "modules are defined in profile" do
it "successfully writes new version to modules" do
pending('not yet implemented')
end
end
context "modules are defined in modules" do
it "successfully writes new version to modules" do
pending('not yet implemented')
end
end
end
end
#-------------------------
results
1) bump version pom.xml is valid successfully writes new version
Failure/Error: write_version('pom.xml', '1.4.0')
Double "file" received :write with unexpected arguments
expected: ("blah")
got: (<UNDEFINED> ... </>)
# templates/scripts/maven_functions.rb.erb:38:in `block in write_file'
# templates/scripts/maven_functions.rb.erb:38:in `write_file'
# templates/scripts/maven_functions.rb.erb:9:in `write_version'
# ./spec/job_creator/maven_functions_spec.rb:64:in `block (3 levels) in <top (required)>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment