githug 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in gitscrub.gemspec
3
+ # Specify your gem's dependencies in githug.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -9,7 +9,7 @@ To install Githug
9
9
 
10
10
  gem install githug
11
11
 
12
- After the gem is installed, you can run `githug` where you will be prompted to create a directory.
12
+ After the gem is installed, you can run `githug` where you will be prompted to create a directory. Githug should work on Linux and OS X.
13
13
 
14
14
  ##Commands
15
15
 
@@ -0,0 +1,25 @@
1
+ difficulty 3
2
+ description "You forgot to branch at the previous commit and made a commit on top of it. Create branch 'test_branch' at the commit before the last"
3
+
4
+ setup do
5
+ repo.init
6
+ FileUtils.touch("file1")
7
+ repo.add("file1")
8
+ repo.commit_all("Adding file1")
9
+ File.open("file1", 'w') { |f| f.write("content") }
10
+ repo.add("file1")
11
+ repo.commit_all("Updating file1")
12
+ File.open("file1", 'a') { |f| f.write("\nAdding some more text") }
13
+ repo.add("file1")
14
+ repo.commit_all("Updating file1 again")
15
+ end
16
+
17
+ solution do
18
+ solved = false
19
+ solved = true if repo.branches.map(&:name).include?("test_branch") and (repo.commits("test_branch").last.message == "Updating file1")
20
+ solved
21
+ end
22
+
23
+ hint do
24
+ puts "Just like creating a branch, but you have to pass an extra argument"
25
+ end
@@ -1,5 +1,5 @@
1
1
  difficulty 3
2
- description "Contribute to this repository by making a pull request on Github"
2
+ description "This is the final level, the goal is to contribute to this repository by making a pull request on Github. Please note that this level is designed to encourage you to add a valid contribution to Githug, not testing your ability to create a pull request. Contributions that are likely to be accepted are levels, bug fixes and improved documentation."
3
3
 
4
4
  solution do
5
5
  location = "/tmp/githug"
@@ -0,0 +1,18 @@
1
+ difficulty 2
2
+
3
+ description "We have a git repo and we want to tag the current commit with new_tag."
4
+
5
+ setup do
6
+ repo.init
7
+ FileUtils.touch("somefile.txt")
8
+ repo.add("somefile.txt")
9
+ repo.commit_all("Added some file to the repo")
10
+ end
11
+
12
+ solution do
13
+ repo.tags.first.name == "new_tag"
14
+ end
15
+
16
+ hint do
17
+ puts "Take a look at `git tag`"
18
+ end
@@ -15,6 +15,17 @@ module Githug
15
15
  game.play_level
16
16
  end
17
17
 
18
+ desc :test, "Test a level from a file path"
19
+ method_option :errors, :type => :boolean, :default => false
20
+
21
+ def test(path)
22
+ UI.word_box("Githug")
23
+ make_directory
24
+ level = Level.load_from_file(path)
25
+ game = Game.new
26
+ game.test_level(level, options[:errors])
27
+ end
28
+
18
29
  desc :hint, "Get a hint for the current level"
19
30
 
20
31
  def hint
@@ -25,12 +36,19 @@ module Githug
25
36
 
26
37
  desc :reset, "Reset the current level"
27
38
 
28
- def reset
29
- if level = load_level
30
- UI.word_box("Githug")
39
+ def reset(path = nil)
40
+ if path
41
+ level = Level.load_from_file(path)
42
+ else
43
+ level = load_level
44
+ end
45
+ UI.word_box("Githug")
46
+ if level
31
47
  UI.puts("resetting level")
32
48
  level.setup_level
33
49
  level.full_description
50
+ else
51
+ UI.error("Level does not exist")
34
52
  end
35
53
  end
36
54
 
@@ -34,9 +34,15 @@ module Githug
34
34
  end
35
35
  end
36
36
 
37
- def dry_run(level)
38
- level = Level.load(level)
39
-
37
+ def test_level(level, errors = nil)
38
+ UI.puts level.full_description
39
+ method = :solve
40
+ method = :test if errors
41
+ if level.send(method)
42
+ UI.success "Valid solution"
43
+ else
44
+ UI.error "Invalid solution"
45
+ end
40
46
  end
41
47
 
42
48
  def level_bump
@@ -4,21 +4,31 @@ module Githug
4
4
 
5
5
  LEVELS = [nil, "init", "add", "commit", "config", "clone",
6
6
  "clone_to_folder", "ignore", "status", "rm", "rm_cached", "rename",
7
- "log", "commit_ammend", "reset", "checkout_file", "remote",
7
+ "log", "tag", "commit_ammend", "reset", "checkout_file", "remote",
8
8
  "remote_url", "remote_add", "diff", "blame", "branch", "checkout",
9
- "merge", "squash", "contribute"]
9
+ "branch_at", "merge", "squash", "contribute"]
10
10
 
11
11
  attr_accessor :level_no, :level_path
12
12
 
13
13
  class << self
14
14
 
15
15
  def load(level_name)
16
+ path = "#{File.dirname(__FILE__)}/../../levels/#{level_name}.rb"
17
+ setup(path)
18
+ end
19
+
20
+ def load_from_file(path)
21
+ setup(path)
22
+ end
23
+
24
+ def setup(path)
25
+ level_name = File.basename(path, File.extname(path))
26
+ #Remove .rb extension, WTB a better way to do this
27
+ level_path = path[0..-4]
16
28
  level = new
17
- level_path = "#{File.dirname(__FILE__)}/../../levels/#{level_name}"
18
- location = "#{level_path}.rb"
19
- return false unless File.exists?(location)
20
- level.instance_eval(File.read(location))
21
- level.level_no = LEVELS.index(level_name)
29
+ return false unless File.exists?(path)
30
+ level.instance_eval(File.read(path))
31
+ level.level_no = LEVELS.index(level_name) || 1
22
32
  level.level_path = level_path
23
33
  level
24
34
  end
@@ -78,6 +88,10 @@ module Githug
78
88
  false
79
89
  end
80
90
 
91
+ def test
92
+ @solution.call
93
+ end
94
+
81
95
  def show_hint
82
96
  UI.word_box("Githug")
83
97
  profile = Profile.load
@@ -0,0 +1,120 @@
1
+ module Githug
2
+ class Level
3
+ include UI
4
+
5
+ LEVELS = [nil, "init", "add", "commit", "config", "clone",
6
+ "clone_to_folder", "ignore", "status", "rm", "rm_cached", "rename",
7
+ <<<<<<< HEAD
8
+ "log", "tag", "commit_ammend", "reset", "checkout_file", "remote",
9
+ "remote_url", "remote_add", "diff", "blame", "branch", "checkout",
10
+ =======
11
+ "log", "commit_ammend", "reset", "checkout_file", "remote",
12
+ "remote_url", "remote_add", "diff", "blame", "branch", "checkout", "branch_at",
13
+ >>>>>>> manojlds/master
14
+ "merge", "squash", "contribute"]
15
+
16
+ attr_accessor :level_no, :level_path
17
+
18
+ class << self
19
+
20
+ def load(level_name)
21
+ path = "#{File.dirname(__FILE__)}/../../levels/#{level_name}.rb"
22
+ setup(path)
23
+ end
24
+
25
+ def load_from_file(path)
26
+ setup(path)
27
+ end
28
+
29
+ def setup(path)
30
+ level_name = File.basename(path, File.extname(path))
31
+ #Remove .rb extension, WTB a better way to do this
32
+ level_path = path[0..-4]
33
+ level = new
34
+ return false unless File.exists?(path)
35
+ level.instance_eval(File.read(path))
36
+ level.level_no = LEVELS.index(level_name) || 1
37
+ level.level_path = level_path
38
+ level
39
+ end
40
+
41
+ end
42
+
43
+ def init_from_level
44
+ FileUtils.cp_r("#{level_path}/.", ".")
45
+ FileUtils.mv(".githug", ".git")
46
+ end
47
+
48
+ def difficulty(num)
49
+ @difficulty = num
50
+ end
51
+
52
+ def description(description)
53
+ @description = description
54
+ end
55
+
56
+ def solution(&block)
57
+ @solution = block
58
+ end
59
+
60
+ def setup(&block)
61
+ @setup = block
62
+ end
63
+
64
+ def hint(&hint)
65
+ @hint = hint
66
+ end
67
+
68
+ def hints(hints)
69
+ @hints = hints
70
+ end
71
+
72
+ def full_description
73
+ UI.puts
74
+ UI.puts "Level: #{level_no}"
75
+ UI.puts "Difficulty: #{"*"*@difficulty}"
76
+ UI.puts
77
+ UI.puts @description
78
+ UI.puts
79
+ end
80
+
81
+ def setup_level
82
+ repo.reset
83
+ @setup.call if @setup
84
+ end
85
+
86
+ def repo(location = "")
87
+ @repo ||= Repository.new(location)
88
+ end
89
+
90
+ def solve
91
+ @solution.call
92
+ rescue
93
+ false
94
+ end
95
+
96
+ def test
97
+ @solution.call
98
+ end
99
+
100
+ def show_hint
101
+ UI.word_box("Githug")
102
+ profile = Profile.load
103
+ current_hint_index = profile.current_hint_index
104
+ if @hints
105
+ puts @hints[current_hint_index]
106
+ if current_hint_index < @hints.size - 1
107
+ profile.current_hint_index += 1
108
+ profile.save
109
+ else
110
+ profile.current_hint_index = 0
111
+ profile.save
112
+ end
113
+ elsif @hint
114
+ @hint.call
115
+ else
116
+ UI.puts("No hints available for this level")
117
+ end
118
+ end
119
+ end
120
+ end
@@ -1,3 +1,3 @@
1
1
  module Githug
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -40,6 +40,18 @@ describe Githug::CLI do
40
40
  lambda {@cli.make_directory}.should raise_error(SystemExit)
41
41
  end
42
42
 
43
+ describe "test" do
44
+ it "should perform a test run of the level" do
45
+ level = mock
46
+ game = mock
47
+ @cli.stub(:make_directory)
48
+ Githug::Level.should_receive(:load_from_file).with("/foo/bar/test/level.rb").and_return(level)
49
+ Githug::Game.stub(:new).and_return(game)
50
+ game.should_receive(:test_level).with(level, anything)
51
+ @cli.test("/foo/bar/test/level.rb")
52
+ end
53
+ end
54
+
43
55
  describe "level methods" do
44
56
  before(:each) do
45
57
  @level = mock
@@ -47,6 +59,7 @@ describe Githug::CLI do
47
59
  @profile.stub(:level).and_return(1)
48
60
  Githug::Profile.stub(:load).and_return(@profile)
49
61
  Githug::Level.stub(:load).and_return(@level)
62
+ Githug::Level.stub(:load_from_file).with("/foo/bar/level.rb").and_return(@level)
50
63
  end
51
64
 
52
65
  it "should call the hint method on the level" do
@@ -69,10 +82,17 @@ describe Githug::CLI do
69
82
  Githug::Level.stub(:load).and_return(false)
70
83
  @level.should_not_receive(:setup_level)
71
84
  @level.should_not_receive(:full_description)
72
- Githug::UI.should_not_receive(:word_box).with("Githug")
73
- Githug::UI.should_not_receive(:puts).with("resetting level")
85
+ Githug::UI.should_receive(:error).with("Level does not exist")
74
86
  @cli.reset
75
87
  end
88
+
89
+ it "should reset the level with a path" do
90
+ @level.should_receive(:setup_level)
91
+ @level.should_receive(:full_description)
92
+ Githug::UI.should_receive(:word_box).with("Githug")
93
+ Githug::UI.should_receive(:puts).with("resetting level")
94
+ @cli.reset("/foo/bar/level.rb")
95
+ end
76
96
  end
77
97
 
78
98
  end
@@ -29,7 +29,7 @@ describe Githug::Game do
29
29
  @game.play_level
30
30
  end
31
31
 
32
- describe "solve" do
32
+ describe "play_level" do
33
33
 
34
34
  it "should echo congratulations if the level is solved" do
35
35
  @level.stub(:solve).and_return(true)
@@ -62,10 +62,22 @@ describe Githug::Game do
62
62
  end
63
63
 
64
64
 
65
- describe "dry run" do
66
- it "should play the game without altering the profile" do
67
- Level.should_receive(:load)
68
- @level.dry_run
65
+ describe "test_level" do
66
+ it "Should output Valid solution if the solution is valid" do
67
+ @level.stub(:solve).and_return(true)
68
+ Githug::UI.should_receive(:success).with("Valid solution")
69
+ @game.test_level(@level)
70
+ end
71
+
72
+ it "Should output Invalid solution if the solution is invalid" do
73
+ @level.stub(:solve).and_return(false)
74
+ Githug::UI.should_receive(:error).with("Invalid solution")
75
+ @game.test_level(@level)
76
+ end
77
+
78
+ it "should call test when errors is true" do
79
+ @level.should_receive(:test)
80
+ @game.test_level(@level, true)
69
81
  end
70
82
  end
71
83
 
@@ -54,6 +54,30 @@ end
54
54
 
55
55
  end
56
56
 
57
+ describe "load_from_file" do
58
+ it "should load the level" do
59
+ File.stub(:dirname).and_return("")
60
+ File.should_receive(:read).with('/foo/bar/test/level.rb').and_return(@file)
61
+ level = Githug::Level.load_from_file("/foo/bar/test/level.rb")
62
+ level.instance_variable_get("@difficulty").should eql(1)
63
+ level.instance_variable_get("@description").should eql("A test description")
64
+ end
65
+
66
+ it "should return false if the level does not exist" do
67
+ File.stub(:exists?).and_return(false)
68
+ Githug::Level.load_from_file("/foo/bar/test/level.rb").should eql(false)
69
+ end
70
+ end
71
+
72
+ describe "setup" do
73
+
74
+ it "should return false if the level does not exist" do
75
+ File.stub(:exists?).and_return(false)
76
+ Githug::Level.setup("/foo/bar/test/level.rb").should eql(false)
77
+ end
78
+
79
+ end
80
+
57
81
 
58
82
  describe "solve" do
59
83
 
@@ -68,6 +92,13 @@ end
68
92
 
69
93
  end
70
94
 
95
+ describe "test" do
96
+ it "should call solve" do
97
+ @level.instance_variable_get("@solution").should_receive(:call)
98
+ @level.test
99
+ end
100
+ end
101
+
71
102
 
72
103
  describe "full_description" do
73
104
 
@@ -148,4 +179,4 @@ end
148
179
  @level.init_from_level
149
180
  end
150
181
  end
151
- end
182
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: githug
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.5
5
+ version: 0.1.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Gary Rennie
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-03-16 00:00:00 +00:00
13
+ date: 2012-03-18 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -113,6 +113,7 @@ files:
113
113
  - levels/blame/.githug/refs/heads/master
114
114
  - levels/blame/config.rb
115
115
  - levels/branch.rb
116
+ - levels/branch_at.rb
116
117
  - levels/checkout.rb
117
118
  - levels/checkout_file.rb
118
119
  - levels/clone.rb
@@ -185,10 +186,12 @@ files:
185
186
  - levels/rm_cached.rb
186
187
  - levels/squash.rb
187
188
  - levels/status.rb
189
+ - levels/tag.rb
188
190
  - lib/githug.rb
189
191
  - lib/githug/cli.rb
190
192
  - lib/githug/game.rb
191
193
  - lib/githug/level.rb
194
+ - lib/githug/level.rb.orig
192
195
  - lib/githug/profile.rb
193
196
  - lib/githug/repository.rb
194
197
  - lib/githug/ui.rb