et 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f73dea1999d7fc46db767ea705e6022aceb45a24
4
- data.tar.gz: 29df46b104d1484f458a5b9e60346a219544b4b7
3
+ metadata.gz: d678e6b3797d0340afda2997d3e23a8d4aad1a41
4
+ data.tar.gz: 00a791674220af78c744b9c7fff5a45f2e379c37
5
5
  SHA512:
6
- metadata.gz: 4835233905faf16607d6fd5a37d264e814827b5be95b4477d43dd04588eaf3dfc03c6104be971f90a7e212a89c068e19dc51992c07582bcbd688c81f7694ac35
7
- data.tar.gz: 14555a87bf0bd86fef92fcbb5256e7bd4cc82098c653d69566fc3f8aaae99f9dcf3232d34095228430b3e01c37eea759d63b8e7791f54dc59d9c29695cd66cd7
6
+ metadata.gz: 239edb45e57abd02f9f9a0df443ae39402b3fc985faa3e317b413ffda27fcb16f4cbd51a45d44495ceb5de118eb93bd82c81bbdf41aff7232aa52cf87a34274e
7
+ data.tar.gz: 055589ff2ea9ca2bf5c4fe0854ffbbc7622103bf8702fc11b3b0ec6854ea45006bc191639b885f781a896dfe31497f60f3c2b3227f5601b8a4bb1cde7d3ff26a
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- et (0.0.1)
4
+ et (0.2.0)
5
5
  gli (= 2.11.0)
6
- rest-client (~> 1.7.2)
6
+ rest-client (~> 1.7)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
@@ -34,6 +34,6 @@ PLATFORMS
34
34
 
35
35
  DEPENDENCIES
36
36
  et!
37
- rake (~> 10.3)
38
- rspec (~> 3.0.0)
39
- rspec-mocks (~> 3.0.0)
37
+ rake (~> 10)
38
+ rspec (~> 3.0)
39
+ rspec-mocks (~> 3.0)
data/lib/et/api.rb CHANGED
@@ -41,17 +41,11 @@ module ET
41
41
  dest
42
42
  end
43
43
 
44
- def submit_challenge(dir)
45
- Dir.mktmpdir do |tmpdir|
46
- slug = File.basename(dir)
47
-
48
- submission_file = File.join(tmpdir, "submission.tar.gz")
49
- if system("tar zcf #{submission_file} -C #{dir} .")
50
- RestClient.post(submission_url(slug),
51
- { submission: { archive: File.new(submission_file) }},
52
- { "Authorization" => auth_header })
53
- end
54
- end
44
+ def submit_challenge(challenge)
45
+ submission_file = challenge.archive!
46
+ RestClient.post(submission_url(challenge.slug),
47
+ { submission: { archive: File.new(submission_file) }},
48
+ { "Authorization" => auth_header })
55
49
  end
56
50
 
57
51
  private
@@ -0,0 +1,69 @@
1
+ require "securerandom"
2
+
3
+ module ET
4
+ class Challenge
5
+ attr_reader :cwd
6
+
7
+ def initialize(cwd)
8
+ @cwd = cwd
9
+ end
10
+
11
+ def archive!
12
+ if exists?
13
+ filepath = random_archive_path
14
+
15
+ cmd = "tar zcf #{filepath} -C #{dir} . --exclude='.challenge'"
16
+
17
+ ignored_files.each do |file|
18
+ cmd += " --exclude='#{file}'"
19
+ end
20
+
21
+ if system(cmd)
22
+ filepath
23
+ else
24
+ nil
25
+ end
26
+ else
27
+ nil
28
+ end
29
+ end
30
+
31
+ def dir
32
+ @dir ||= find_challenge_dir(cwd)
33
+ end
34
+
35
+ def slug
36
+ File.basename(dir)
37
+ end
38
+
39
+ def exists?
40
+ !dir.nil?
41
+ end
42
+
43
+ def ignored_files
44
+ config["ignore"] || []
45
+ end
46
+
47
+ private
48
+
49
+ def config
50
+ @config ||= YAML.load(File.read(File.join(dir, ".challenge")))
51
+ end
52
+
53
+ def random_archive_path
54
+ File.join(Dir.mktmpdir, "#{SecureRandom.hex}.tar.gz")
55
+ end
56
+
57
+ def find_challenge_dir(current_dir)
58
+ path = File.join(current_dir, ".challenge")
59
+
60
+ if File.exists?(path)
61
+ current_dir
62
+ elsif current_dir == "/" || current_dir == "."
63
+ nil
64
+ else
65
+ find_challenge_dir(File.dirname(current_dir))
66
+ end
67
+ end
68
+ end
69
+ end
data/lib/et/runner.rb CHANGED
@@ -32,6 +32,8 @@ module ET
32
32
 
33
33
  settings = prompt_for_missing(settings)
34
34
  save_config(settings)
35
+
36
+ puts "Saved configuration to #{config.path}"
35
37
  end
36
38
  end
37
39
 
@@ -49,8 +51,13 @@ module ET
49
51
  challenge = api.get_challenge(slug)
50
52
  archive = api.download_file(challenge[:archive_url])
51
53
 
52
- system("tar zxf #{archive} -C #{cwd}")
53
- system("rm #{archive}")
54
+ if system("tar zxf #{archive} -C #{cwd}")
55
+ system("rm #{archive}")
56
+ challenge_dir = File.join(cwd, slug)
57
+ puts "Extracted challenge to #{challenge_dir}"
58
+ else
59
+ raise StandardError.new("Failed to extract the challenge archive.")
60
+ end
54
61
  end
55
62
  end
56
63
  end
@@ -58,7 +65,14 @@ module ET
58
65
  desc "Submit the challenge in this directory."
59
66
  command :submit do |c|
60
67
  c.action do |_global_options, _options, _cmdargs|
61
- api.submit_challenge(cwd)
68
+ challenge = Challenge.new(cwd)
69
+
70
+ if challenge.exists?
71
+ api.submit_challenge(challenge)
72
+ puts "Challenge submitted"
73
+ else
74
+ raise StandardError.new("Not in a challenge directory.")
75
+ end
62
76
  end
63
77
  end
64
78
 
data/lib/et/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module ET
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/et.rb CHANGED
@@ -3,3 +3,4 @@ require "et/runner"
3
3
  require "et/api"
4
4
  require "et/config"
5
5
  require "et/formatter"
6
+ require "et/challenge"
@@ -10,7 +10,6 @@ describe "submit challenge" do
10
10
  challenge_dir = write_sample_challenge_to(tmpdir, "some-challenge")
11
11
 
12
12
  expect_any_instance_of(ET::API).to receive(:submit_challenge).
13
- with(challenge_dir).
14
13
  and_return(true)
15
14
 
16
15
  runner = ET::Runner.new(challenge_dir)
@@ -0,0 +1,91 @@
1
+ require "yaml"
2
+
3
+ describe ET::Challenge do
4
+ let(:challenge_info) do
5
+ {
6
+ "title" => "Guess the Number",
7
+ "slug" => "guess-the-number",
8
+ "ignore" => ["README.md"]
9
+ }
10
+ end
11
+
12
+ describe "#dir" do
13
+ it "selects the directory containing the challenge file" do
14
+ Dir.mktmpdir do |challenge_dir|
15
+ challenge_path = File.join(challenge_dir, ".challenge")
16
+ File.write(challenge_path, challenge_info.to_yaml)
17
+
18
+ challenge = ET::Challenge.new(challenge_dir)
19
+ expect(challenge.dir).to eq(challenge_dir)
20
+ end
21
+ end
22
+
23
+ it "checks parent directories for the challenge file" do
24
+ Dir.mktmpdir do |parent_dir|
25
+ challenge_path = File.join(parent_dir, ".challenge")
26
+ File.write(challenge_path, challenge_info.to_yaml)
27
+
28
+ child_dir = File.join(parent_dir, "foobar")
29
+
30
+ challenge = ET::Challenge.new(child_dir)
31
+ expect(challenge.dir).to eq(parent_dir)
32
+ end
33
+ end
34
+
35
+ it "returns nil if no challenge file found" do
36
+ challenge = ET::Challenge.new(Dir.tmpdir)
37
+ expect(challenge.dir).to eq(nil)
38
+ end
39
+ end
40
+
41
+ describe "#archive" do
42
+ it "packages up files in the given directory" do
43
+ archive_path = nil
44
+
45
+ begin
46
+ Dir.mktmpdir do |challenge_dir|
47
+ challenge_path = File.join(challenge_dir, ".challenge")
48
+ File.write(challenge_path, challenge_info.to_yaml)
49
+
50
+ file_path = File.join(challenge_dir, "file.rb")
51
+ File.write(file_path, "2 + 2 == 5")
52
+
53
+ challenge = ET::Challenge.new(challenge_dir)
54
+ archive_path = challenge.archive!
55
+
56
+ contents = read_file_from_gzipped_archive(archive_path, "./file.rb")
57
+ expect(contents).to eq("2 + 2 == 5")
58
+ end
59
+ ensure
60
+ if archive_path && File.exist?(archive_path)
61
+ FileUtils.rm(archive_path)
62
+ end
63
+ end
64
+ end
65
+
66
+ it "excludes files in the ignore array" do
67
+ archive_path = nil
68
+
69
+ begin
70
+ Dir.mktmpdir do |dir|
71
+ File.write(File.join(dir, ".challenge"), challenge_info.to_yaml)
72
+ File.write(File.join(dir, "file.rb"), "2 + 2 == 5")
73
+ File.write(File.join(dir, "README.md"), "Ignore me!")
74
+
75
+ challenge = ET::Challenge.new(dir)
76
+ archive_path = challenge.archive!
77
+
78
+ files = list_files_in_gzipped_archive(archive_path)
79
+
80
+ expect(files).to include("./file.rb")
81
+ expect(files).to_not include("./README.md")
82
+ expect(files).to_not include("./.challenge")
83
+ end
84
+ ensure
85
+ if archive_path && File.exist?(archive_path)
86
+ FileUtils.rm(archive_path)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
data/spec/spec_helper.rb CHANGED
@@ -48,6 +48,7 @@ RSpec.configure do |config|
48
48
  config.include OutputCatcher
49
49
  config.include SampleFiles
50
50
  config.include PathHelper
51
+ config.include ArchiveHelper
51
52
 
52
53
  config.before :each do
53
54
  allow(RestClient).to receive(:get)
@@ -0,0 +1,28 @@
1
+ require "zlib"
2
+ require "rubygems/package"
3
+
4
+ module ArchiveHelper
5
+ def read_file_from_gzipped_archive(archive_path, filename)
6
+ Zlib::GzipReader.open(archive_path) do |gz|
7
+ contents = nil
8
+
9
+ Gem::Package::TarReader.new(gz) do |tar|
10
+ contents = tar.seek(filename) { |f| f.read }
11
+ end
12
+
13
+ contents
14
+ end
15
+ end
16
+
17
+ def list_files_in_gzipped_archive(archive_path)
18
+ file_names = nil
19
+
20
+ Zlib::GzipReader.open(archive_path) do |gz|
21
+ Gem::Package::TarReader.new(gz) do |tar|
22
+ file_names = tar.entries.map { |entry| entry.full_name }
23
+ end
24
+ end
25
+
26
+ file_names
27
+ end
28
+ end
@@ -17,12 +17,14 @@ module SampleFiles
17
17
  end
18
18
 
19
19
  def write_sample_challenge_to(working_dir, slug)
20
- challenge_dir = File.join(working_dir, slug)
21
- system("mkdir #{challenge_dir}")
20
+ options = { "title" => slug.capitalize, "slug" => slug }
22
21
 
23
- readme_path = File.join(challenge_dir, "README.md")
24
- File.write(readme_path, "# README")
22
+ dir = File.join(working_dir, slug)
23
+ system("mkdir #{dir}")
25
24
 
26
- challenge_dir
25
+ File.write(File.join(dir, "README.md"), "# README")
26
+ File.write(File.join(dir, ".challenge"), options.to_yaml)
27
+
28
+ dir
27
29
  end
28
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: et
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Sheehan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-13 00:00:00.000000000 Z
11
+ date: 2014-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -102,6 +102,7 @@ files:
102
102
  - et.gemspec
103
103
  - lib/et.rb
104
104
  - lib/et/api.rb
105
+ - lib/et/challenge.rb
105
106
  - lib/et/config.rb
106
107
  - lib/et/formatter.rb
107
108
  - lib/et/runner.rb
@@ -113,8 +114,10 @@ files:
113
114
  - spec/data/challenges.json
114
115
  - spec/data/some-challenge.tar.gz
115
116
  - spec/lib/api_spec.rb
117
+ - spec/lib/challenge_spec.rb
116
118
  - spec/lib/config_spec.rb
117
119
  - spec/spec_helper.rb
120
+ - spec/support/helpers/archive_helper.rb
118
121
  - spec/support/helpers/output_catcher.rb
119
122
  - spec/support/helpers/path_helper.rb
120
123
  - spec/support/helpers/sample_files.rb