Skip to content

Commit

Permalink
Merge pull request crystal-lang#148 from luislavena/add-version-command
Browse files Browse the repository at this point in the history
Introduces `version` command to retrieve shards current version
  • Loading branch information
ysbaddaden authored Feb 9, 2017
2 parents a2743f1 + ec96c2b commit 105cf05
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module Shards
puts " prune"
#puts " search <query>"
puts " update"
#puts " update [package package ...]"
# puts " update [package package ...]"
puts " version [path]"
puts
puts "Options:"
puts opts
Expand Down Expand Up @@ -50,12 +51,14 @@ module Shards
Commands::List.run(path)
when "prune"
Commands::Prune.run(path)
#when "search"
# display_help_and_exit(opts) unless args[1]?
# Commands::Search.run(path, args[1])
# when "search"
# display_help_and_exit(opts) unless args[1]?
# Commands::Search.run(path, args[1])
when "update"
Commands::Update.run(path)
#Commands.update(path, *args[1..-1])
# Commands.update(path, *args[1..-1])
when "version"
Commands::Version.run(args[1]? || path)
else
display_help_and_exit(opts)
end
Expand Down
33 changes: 33 additions & 0 deletions src/commands/version.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "../helpers/versions"
require "./command"

module Shards
module Commands
class Version < Command
def self.run(path, *args)
path = lookup_path(path)
new(path).run(*args)
end

def run(*args)
puts spec.version
end

# look up for `SPEC_FILENAME` in *path* or up
private def self.lookup_path(path)
previous = nil
current = File.expand_path(path)

until !File.directory?(current) || current == previous
shard_file = File.join(current, SPEC_FILENAME)
break if File.exists?(shard_file)

previous = current
current = File.dirname(current)
end

current
end
end
end
end
36 changes: 36 additions & 0 deletions test/integration/version_test.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "../integration_helper"

class VersionCommandTest < Minitest::Test
def test_version_default_directory
metadata = {
version: "1.33.7",
}
with_shard(metadata) do
stdout = run "shards version", capture: true
assert_match "1.33.7", stdout
end
end

def test_version_within_directory
metadata = {
version: "0.0.42",
}
with_shard(metadata) do
inner_path = File.join(application_path, "lib/test")
Dir.mkdir_p inner_path

outer_path = File.expand_path("..", application_path)
Dir.cd(outer_path) do
stdout = run "shards version #{inner_path}", capture: true
assert_match "0.0.42", stdout
end
end
end

def test_fails_version
ex = assert_raises(FailedCommand) do
root = File.expand_path("/", Dir.current)
run "shards version #{root}"
end
end
end

0 comments on commit 105cf05

Please sign in to comment.