gitscrub 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,6 @@ difficulty 1
2
2
  description "There is a file in your folder called README, you should add it to your staging area"
3
3
 
4
4
  setup do
5
- `rm -rf .git`
6
5
  `git init`
7
6
  `touch README`
8
7
  end
data/levels/commit.rb ADDED
@@ -0,0 +1,14 @@
1
+ difficulty 1
2
+ description "Make a commit"
3
+
4
+ setup do
5
+ `git init`
6
+ `touch README`
7
+ `git add README`
8
+ end
9
+
10
+ solution do
11
+ repo = Grit::Repo.new(".")
12
+ return false if repo.commits.empty?
13
+ true
14
+ end
@@ -0,0 +1,21 @@
1
+ difficulty 3
2
+ description "Contribute to this repository by making a pull request on Github"
3
+
4
+ solution do
5
+ location = "/tmp/gitscrub"
6
+ FileUtils.rm_rf(location)
7
+ puts "Cloning repository to #{location}"
8
+ `git clone git@github.com:Gazler/gitscrub.git #{location}`
9
+
10
+ contributor = false
11
+
12
+ repo = Grit::Repo.new(location)
13
+ repo.commits.each do |commit|
14
+ if commit.author.name == repo.config["user.name"]
15
+ if commit.author.email == repo.config["user.email"]
16
+ contributor = true
17
+ end
18
+ end
19
+ end
20
+ contributor
21
+ end
@@ -1,10 +1,6 @@
1
1
  difficulty 1
2
2
  description "Initialize an empty repository"
3
3
 
4
- setup do
5
- `rm -rf .git`
6
- end
7
-
8
4
  solution do
9
5
  Grit::Repo.new(".")
10
6
  end
data/lib/gitscrub.rb CHANGED
@@ -5,6 +5,7 @@ require 'gitscrub/ui'
5
5
  require 'gitscrub/game'
6
6
  require 'gitscrub/profile'
7
7
  require 'gitscrub/level'
8
+ require 'gitscrub/repository'
8
9
 
9
10
  Gitscrub::UI.in_stream = STDIN
10
11
  Gitscrub::UI.out_stream = STDOUT
data/lib/gitscrub/cli.rb CHANGED
@@ -37,10 +37,18 @@ module Gitscrub
37
37
  no_tasks do
38
38
 
39
39
  def make_directory
40
- unless File.exists?("./git_scrub") || Dir.pwd.split("/").last == "git_scrub"
40
+ if File.exists?("./git_scrub")
41
+ UI.puts "Please change into the git_scrub directory"
42
+ exit
43
+ end
44
+
45
+ unless File.basename(Dir.pwd) == "git_scrub"
41
46
  if UI.ask("No gitscrub directory found, do you wish to create one?")
42
47
  Dir.mkdir("./git_scrub")
43
48
  Dir.chdir("git_scrub")
49
+ File.open(".gitignore", "w") do |file|
50
+ file.write(".profile.yml")
51
+ end
44
52
  else
45
53
  UI.puts("Exiting")
46
54
  exit
data/lib/gitscrub/game.rb CHANGED
@@ -17,10 +17,10 @@ module Gitscrub
17
17
  level = Level.load(profile.level)
18
18
  if solve && level
19
19
  if level.solve
20
- UI.puts "Congratulations, you have solved the level"
20
+ UI.success "Congratulations, you have solved the level"
21
21
  level_bump
22
22
  else
23
- UI.puts "Sorry, this solution is not quite right!"
23
+ UI.error "Sorry, this solution is not quite right!"
24
24
  UI.puts level.full_description
25
25
  end
26
26
  end
@@ -1,13 +1,16 @@
1
1
  module Gitscrub
2
2
  class Level
3
+ include UI
3
4
 
4
- attr_accessor :ldifficulty, :ldescription, :lsolution, :level_no, :lsetup
5
+ LEVELS = [nil, "init", "add", "commit", "contribute"]
6
+
7
+ attr_accessor :level_no
5
8
 
6
9
  class << self
7
10
 
8
11
  def load(level_no)
9
12
  level = new
10
- location = "#{File.dirname(__FILE__)}/../../levels/#{level_no}.rb"
13
+ location = "#{File.dirname(__FILE__)}/../../levels/#{LEVELS[level_no]}.rb"
11
14
  return false unless File.exists?(location)
12
15
  level.instance_eval(File.read(location))
13
16
  level.level_no = level_no
@@ -17,36 +20,41 @@ module Gitscrub
17
20
  end
18
21
 
19
22
  def difficulty(num)
20
- @ldifficulty = num
23
+ @difficulty = num
21
24
  end
22
25
 
23
26
  def description(description)
24
- @ldescription = description
27
+ @description = description
25
28
  end
26
29
 
27
30
  def solution(&block)
28
- @lsolution = block
31
+ @solution = block
29
32
  end
30
33
 
31
34
  def setup(&block)
32
- @lsetup = block
35
+ @setup = block
33
36
  end
34
37
 
35
38
  def full_description
36
39
  UI.puts
37
40
  UI.puts "Level: #{level_no}"
38
- UI.puts "Difficulty: #{"*"*ldifficulty}"
41
+ UI.puts "Difficulty: #{"*"*@difficulty}"
39
42
  UI.puts
40
- UI.puts ldescription
43
+ UI.puts @description
41
44
  UI.puts
42
45
  end
43
46
 
44
47
  def setup_level
45
- lsetup.call
48
+ repo.reset
49
+ @setup.call if @setup
50
+ end
51
+
52
+ def repo
53
+ @repo ||= Repository.new
46
54
  end
47
55
 
48
56
  def solve
49
- lsolution.call
57
+ @solution.call
50
58
  rescue
51
59
  false
52
60
  end
@@ -0,0 +1,23 @@
1
+ module Gitscrub
2
+ class Repository
3
+
4
+ attr_accessor :grit
5
+
6
+ def initialize
7
+ @grit = Grit::Repo.new(".")
8
+ rescue Grit::InvalidGitRepositoryError
9
+ @grit = nil
10
+ end
11
+
12
+ def reset
13
+ dont_delete = ["..", ".", ".gitignore", ".profile.yml"]
14
+ if File.basename(Dir.pwd) == "git_scrub"
15
+ Dir.entries(Dir.pwd).each do |file|
16
+ FileUtils.rm_rf(file) unless dont_delete.include?(file)
17
+ end
18
+ end
19
+ end
20
+
21
+
22
+ end
23
+ end
data/lib/gitscrub/ui.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Gitscrub
2
- class UI
2
+ module UI
3
3
 
4
4
  @@out_stream
5
5
  @@in_stream
@@ -50,6 +50,23 @@ module Gitscrub
50
50
  request("#{msg} [yn] ") == 'y'
51
51
  end
52
52
 
53
+ def colorize(text, color_code)
54
+ puts "#{color_code}#{text}\033[0m"
55
+ end
56
+
57
+ def error(text)
58
+ colorize(text, "\033[31m")
59
+ end
60
+
61
+ def success(text)
62
+ colorize(text, "\033[32m")
63
+ end
64
+
65
+ end
66
+
67
+ def method_missing(method, *args, &block)
68
+ return UI.send(method, *args) if UI.methods(false).include?(method)
69
+ super
53
70
  end
54
71
 
55
72
  end
@@ -1,3 +1,3 @@
1
1
  module Gitscrub
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -19,6 +19,7 @@ describe Gitscrub::CLI do
19
19
  Gitscrub::UI.stub(:ask).and_return(true)
20
20
  Dir.should_receive(:mkdir).with("./git_scrub")
21
21
  Dir.should_receive(:chdir).with("git_scrub")
22
+ File.should_receive(:open).with(".gitignore", "w").and_return(true)
22
23
  @cli.make_directory
23
24
  end
24
25
 
@@ -28,11 +29,17 @@ describe Gitscrub::CLI do
28
29
  @cli.make_directory
29
30
  end
30
31
 
31
- it "should create a directory if one does not exist" do
32
+ it "should exit if the user selects no" do
32
33
  Gitscrub::UI.stub(:ask).and_return(false)
33
34
  lambda {@cli.make_directory}.should raise_error(SystemExit)
34
35
  end
35
36
 
37
+ it "should prompt to change into the directory if it exists" do
38
+ File.stub(:exists?).and_return(true)
39
+ Gitscrub::UI.should_receive(:puts).with("Please change into the git_scrub directory")
40
+ lambda {@cli.make_directory}.should raise_error(SystemExit)
41
+ end
42
+
36
43
  it "should check the current solution" do
37
44
  @cli.check.should eql(true)
38
45
  end
@@ -31,13 +31,13 @@ describe Gitscrub::Game do
31
31
  it "should echo congratulations if the level is solved" do
32
32
  @level.stub(:solve).and_return(true)
33
33
  @profile.should_receive(:level=).with(2)
34
- Gitscrub::UI.should_receive(:puts).with("Congratulations, you have solved the level")
34
+ Gitscrub::UI.should_receive(:success).with("Congratulations, you have solved the level")
35
35
  @game.play_level
36
36
  end
37
37
 
38
38
  it "should echo congratulations if the level is solved" do
39
39
  @level.stub(:solve).and_return(false)
40
- Gitscrub::UI.should_receive(:puts).with("Sorry, this solution is not quite right!")
40
+ Gitscrub::UI.should_receive(:error).with("Sorry, this solution is not quite right!")
41
41
  @game.play_level
42
42
  end
43
43
 
@@ -14,43 +14,92 @@ solution do
14
14
  Grit::Repo.new("gitscrub/notadir")
15
15
  end
16
16
  eof
17
+ File.stub(:exists?).and_return(true)
17
18
  File.stub(:read).and_return(@file)
18
19
  @level = Gitscrub::Level.load(1)
20
+ @repo = mock
21
+ @repo.stub(:reset)
22
+ Gitscrub::Repository.stub(:new).and_return(@repo)
19
23
  end
20
24
 
21
- it "should load the level" do
22
- File.stub(:dirname).and_return("")
23
- File.stub(:exists?).and_return(true)
24
- File.should_receive(:read).with('/../../levels/1.rb').and_return(@file)
25
- level = Gitscrub::Level.load(1)
26
- level.ldifficulty.should eql(1)
27
- level.ldescription.should eql("A test description")
25
+ it "should mixin UI" do
26
+ Gitscrub::Level.ancestors.should include(Gitscrub::UI)
28
27
  end
29
28
 
30
- it "should return false if the level does not exist" do
31
- File.stub(:exists?).and_return(false)
32
- Gitscrub::Level.load(1).should eql(false)
29
+
30
+ describe "load" do
31
+
32
+ it "should load the level" do
33
+ File.stub(:dirname).and_return("")
34
+ File.should_receive(:read).with('/../../levels/init.rb').and_return(@file)
35
+ level = Gitscrub::Level.load(1)
36
+ level.instance_variable_get("@difficulty").should eql(1)
37
+ level.instance_variable_get("@description").should eql("A test description")
38
+ end
39
+
40
+ it "should return false if the level does not exist" do
41
+ File.stub(:exists?).and_return(false)
42
+ Gitscrub::Level.load(1).should eql(false)
43
+ end
44
+
33
45
  end
34
46
 
35
- it "should solve the problem" do
36
- @level.solve.should eql(false)
47
+
48
+ describe "solve" do
49
+
50
+ it "should solve the problem" do
51
+ @level.solve.should eql(false)
52
+ end
53
+
54
+ it "should return true if the requirements have been met" do
55
+ Grit::Repo.stub(:new).and_return(true)
56
+ @level.solve.should eql(true)
57
+ end
58
+
37
59
  end
38
60
 
39
- it "should return true if the requirements have been met" do
40
- Grit::Repo.stub(:new).and_return(true)
41
- @level.solve.should eql(true)
61
+
62
+ describe "full_description" do
63
+
64
+ it "should display a full description" do
65
+ Gitscrub::UI.stub(:puts)
66
+ Gitscrub::UI.should_receive(:puts).with("Level: 1")
67
+ Gitscrub::UI.should_receive(:puts).with("Difficulty: *")
68
+ Gitscrub::UI.should_receive(:puts).with("A test description")
69
+ @level.full_description
70
+ end
71
+
42
72
  end
43
73
 
44
- it "should display a full description" do
45
- Gitscrub::UI.stub(:puts)
46
- Gitscrub::UI.should_receive(:puts).with("Level: 1")
47
- Gitscrub::UI.should_receive(:puts).with("Difficulty: *")
48
- Gitscrub::UI.should_receive(:puts).with(@level.ldescription)
49
- @level.full_description
74
+ describe "setup" do
75
+
76
+ it "should call setup" do
77
+ @level.setup_level.should eql("test")
78
+ end
79
+
80
+ it "should not call the setup if none exists" do
81
+ @level.instance_variable_set("@setup", nil)
82
+ lambda {@level.setup_level}.should_not raise_error(NoMethodError)
83
+ end
84
+
50
85
  end
86
+
87
+
88
+ describe "repo" do
89
+
90
+ it "should initialize a repository when repo is called" do
91
+ @level.repo.should equal(@repo)
92
+ Gitscrub::Repository.should_not_receive(:new)
93
+ @level.repo.should equal(@repo)
94
+ end
95
+
96
+ it "should call reset on setup_level" do
97
+ @repo.should_receive(:reset)
98
+ @level.setup_level
99
+ end
51
100
 
52
- it "should call setup" do
53
- @level.setup_level.should eql("test")
54
101
  end
102
+
103
+
55
104
 
56
105
  end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gitscrub::Repository do
4
+
5
+ before(:each) do
6
+ @grit = mock
7
+ Grit::Repo.stub(:new).and_return(@grit)
8
+ @repository = Gitscrub::Repository.new
9
+ end
10
+
11
+ describe "initialize" do
12
+
13
+ it "should call grit on initialize" do
14
+ Grit::Repo.should_receive(:new).and_return(@grit)
15
+ repo = Gitscrub::Repository.new
16
+ repo.grit.should equal(@grit)
17
+ end
18
+
19
+ it "should contain a nil grit if the repo is invalid" do
20
+ Grit::Repo.should_receive(:new).and_raise(Grit::InvalidGitRepositoryError)
21
+ repo = Gitscrub::Repository.new
22
+ repo.grit.should equal(nil)
23
+ end
24
+
25
+ end
26
+
27
+ describe "reset" do
28
+
29
+ before(:each) do
30
+ FileUtils.stub(:rm_rf)
31
+ end
32
+
33
+ it "should do nothing if the current directory isn't git_scrub" do
34
+ Dir.stub(:pwd).and_return("/tmp/foo")
35
+ FileUtils.should_not_receive(:rm_rf)
36
+ @repository.reset
37
+ end
38
+
39
+ it "should remove all the files except .gitignore and .profile.yml" do
40
+ Dir.stub(:pwd).and_return("/tmp/git_scrub")
41
+ Dir.stub(:entries).and_return([".profile.yml", ".gitignore", "..", ".", "README", ".git"])
42
+ FileUtils.should_receive(:rm_rf).with("README")
43
+ FileUtils.should_receive(:rm_rf).with(".git")
44
+ @repository.reset
45
+ end
46
+ end
47
+
48
+
49
+ end
@@ -70,5 +70,15 @@ describe Gitscrub::UI do
70
70
  @ui.stub(:request).and_return('aklhasdf')
71
71
  @ui.ask("foo?").should be_false
72
72
  end
73
+
74
+ it "should print out a success message in green" do
75
+ @ui.success("success")
76
+ @out.string.should eql("\033[32msuccess\033[0m\n")
77
+ end
78
+
79
+ it "should print out a error message in red" do
80
+ @ui.error("error")
81
+ @out.string.should eql("\033[31merror\033[0m\n")
82
+ end
73
83
 
74
84
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: gitscrub
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Gary Rennie
@@ -62,19 +62,23 @@ files:
62
62
  - Rakefile
63
63
  - bin/gitscrub
64
64
  - gitscrub.gemspec
65
- - levels/1.rb
66
- - levels/2.rb
65
+ - levels/add.rb
66
+ - levels/commit.rb
67
+ - levels/contribute.rb
68
+ - levels/init.rb
67
69
  - lib/gitscrub.rb
68
70
  - lib/gitscrub/cli.rb
69
71
  - lib/gitscrub/game.rb
70
72
  - lib/gitscrub/level.rb
71
73
  - lib/gitscrub/profile.rb
74
+ - lib/gitscrub/repository.rb
72
75
  - lib/gitscrub/ui.rb
73
76
  - lib/gitscrub/version.rb
74
77
  - spec/gitscrub/cli_spec.rb
75
78
  - spec/gitscrub/game_spec.rb
76
79
  - spec/gitscrub/level_spec.rb
77
80
  - spec/gitscrub/profile_spec.rb
81
+ - spec/gitscrub/repository_spec.rb
78
82
  - spec/gitscrub/ui_spec.rb
79
83
  - spec/spec_helper.rb
80
84
  has_rdoc: true