social_snippet 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +8 -8
  2. data/.travis.yml +11 -4
  3. data/README.md +6 -6
  4. data/Rakefile +19 -1
  5. data/lib/social_snippet.rb +11 -0
  6. data/lib/social_snippet/api/install_repository_api.rb +23 -82
  7. data/lib/social_snippet/api/manifest_api.rb +3 -3
  8. data/lib/social_snippet/api/update_repository_api.rb +34 -35
  9. data/lib/social_snippet/command_line/sspm/sub_commands/install_command.rb +7 -7
  10. data/lib/social_snippet/config.rb +18 -8
  11. data/lib/social_snippet/core.rb +13 -0
  12. data/lib/social_snippet/document.rb +2 -0
  13. data/lib/social_snippet/document_backend.rb +2 -0
  14. data/lib/social_snippet/document_backend/yaml_document.rb +252 -0
  15. data/lib/social_snippet/document_backend/yaml_document/query.rb +45 -0
  16. data/lib/social_snippet/repository.rb +2 -2
  17. data/lib/social_snippet/repository/driver_factory.rb +42 -0
  18. data/lib/social_snippet/repository/drivers.rb +3 -2
  19. data/lib/social_snippet/repository/drivers/driver_base.rb +100 -0
  20. data/lib/social_snippet/repository/drivers/entry.rb +11 -0
  21. data/lib/social_snippet/repository/drivers/git_driver.rb +100 -0
  22. data/lib/social_snippet/repository/models.rb +15 -0
  23. data/lib/social_snippet/repository/models/package.rb +97 -0
  24. data/lib/social_snippet/repository/models/repository.rb +94 -0
  25. data/lib/social_snippet/repository/repository_manager.rb +79 -67
  26. data/lib/social_snippet/resolvers/base_resolver.rb +13 -17
  27. data/lib/social_snippet/rspec/test_document.rb +305 -0
  28. data/lib/social_snippet/rspec/test_driver.rb +76 -0
  29. data/lib/social_snippet/rspec/test_storage.rb +459 -0
  30. data/lib/social_snippet/storage.rb +1 -0
  31. data/lib/social_snippet/storage_backend.rb +3 -0
  32. data/lib/social_snippet/storage_backend/file_system_storage.rb +71 -0
  33. data/lib/social_snippet/storage_backend/storage_base.rb +35 -0
  34. data/lib/social_snippet/version.rb +8 -3
  35. data/spec/helpers/codeclimate_helper.rb +1 -1
  36. data/spec/helpers/fakefs_helper.rb +10 -0
  37. data/spec/helpers/social_snippet_helper.rb +117 -0
  38. data/spec/lib/api/insert_snippet_spec.rb +95 -2
  39. data/spec/lib/api/update_repository_spec.rb +196 -0
  40. data/spec/lib/command_line/sspm_install_spec.rb +169 -180
  41. data/spec/lib/command_line/sspm_update_spec.rb +56 -0
  42. data/spec/lib/config_spec.rb +6 -8
  43. data/spec/lib/core_spec.rb +21 -38
  44. data/spec/lib/file_system_storage_spec.rb +170 -0
  45. data/spec/lib/insert_resolver_spec.rb +15 -2
  46. data/spec/lib/registry_client_spec.rb +6 -9
  47. data/spec/lib/repository/driver_base_spec.rb +89 -0
  48. data/spec/lib/repository/git_driver_spec.rb +10 -0
  49. data/spec/lib/repository/package_spec.rb +172 -0
  50. data/spec/lib/repository/repository_factory_spec.rb +67 -22
  51. data/spec/lib/repository/repository_manager_spec.rb +71 -114
  52. data/spec/lib/repository/repository_spec.rb +191 -0
  53. data/spec/lib/yaml_document_spec.rb +14 -0
  54. data/spec/spec_helper.rb +8 -93
  55. data/test/config_test.rb +6 -7
  56. data/test/driver_base_test.rb +201 -0
  57. data/test/git_driver_test.rb +51 -0
  58. data/test/insert_snippet_test.rb +256 -349
  59. data/test/repository_manager_test.rb +7 -16
  60. data/test/yaml_document_test.rb +97 -0
  61. metadata +44 -16
  62. data/lib/social_snippet/repository/drivers/base_repository.rb +0 -189
  63. data/lib/social_snippet/repository/drivers/git_repository.rb +0 -74
  64. data/lib/social_snippet/repository/repository_factory.rb +0 -59
  65. data/lib/social_snippet/repository/repository_installer.rb +0 -85
  66. data/spec/lib/repository/base_repository_spec.rb +0 -104
  67. data/spec/lib/repository/git_repository_spec.rb +0 -83
  68. data/spec/lib/repository/repository_installer_spec.rb +0 -63
  69. data/test/base_repository_test.rb +0 -375
  70. data/test/git_repository_test.rb +0 -114
@@ -0,0 +1,14 @@
1
+ require "spec_helper"
2
+
3
+ describe ::SocialSnippet::DocumentBackend::YAMLDocument do
4
+
5
+ context "activate" do
6
+
7
+ before { ::SocialSnippet::DocumentBackend::YAMLDocument.activate! }
8
+
9
+ include_context :TestDocument
10
+
11
+ end
12
+
13
+ end
14
+
@@ -8,99 +8,14 @@ require "cgi"
8
8
  require "stringio"
9
9
 
10
10
  require_relative "helpers/fakefs_helper"
11
+ require_relative "helpers/social_snippet_helper"
12
+ require "social_snippet/rspec/test_document"
13
+ require "social_snippet/rspec/test_storage"
14
+ require "social_snippet/rspec/test_driver"
11
15
 
12
- module SocialSnippet::SpecHelpers
13
-
14
- class Fake; end
15
-
16
- def fake_io
17
- @fake_io ||= StringIO.new
18
- end
19
-
20
- def fake_logger
21
- reset_fake_logger unless @logger
22
- @logger
23
- end
24
-
25
- def reset_fake_logger
26
- @logger = ::SocialSnippet::Logger.new(fake_io)
27
- @logger.level = ::SocialSnippet::Logger::Severity::INFO
28
- end
29
-
30
- def fake_home
31
- reset_fake_home unless @tmp_path
32
- @tmp_path
33
- end
34
-
35
- def reset_fake_home
36
- tmp_root = File.join(Dir.tmpdir, "social_snippet")
37
- FileUtils.mkdir_p tmp_root
38
- @tmp_path = Dir.mktmpdir(nil, tmp_root)
39
- end
40
-
41
- def make_fake_home
42
- fake_config.init_directories
43
- end
44
-
45
- def global_config
46
- $social_snippet_config ||= ::SocialSnippet::Config.new(
47
- fake_core,
48
- {
49
- :home => fake_home,
50
- :sspm_host => "api.server",
51
- },
52
- )
53
- end
54
-
55
- def enable_global_config
56
- @enable_global_config = true
16
+ RSpec.configure do |config|
17
+ config.before(:example, :without_fakefs => true) do
18
+ disable_fakefs
19
+ make_fake_home
57
20
  end
58
-
59
- def fake_config
60
- @config ||= ::SocialSnippet::Config.new(
61
- fake_core,
62
- {
63
- :home => fake_home,
64
- :sspm_host => "api.server",
65
- },
66
- )
67
- end
68
-
69
- def fake_api
70
- @fake_api ||= ::SocialSnippet::Api.new(fake_core)
71
- end
72
-
73
- def fake_core
74
- reset_fake_core unless @fake_core
75
- @fake_core
76
- end
77
-
78
- def reset_fake_core
79
- @fake_core = Fake.new
80
- allow(fake_core).to receive(:logger).and_return fake_logger
81
- allow(fake_core).to receive(:config).and_return fake_config
82
- allow(fake_core).to receive(:api).and_return fake_api
83
- allow(fake_core).to receive(:repo_manager).and_return ::SocialSnippet::Repository::RepositoryManager.new(fake_core)
84
- allow(fake_core).to receive(:registry_client).and_return ::SocialSnippet::Registry::RegistryClient.new(fake_core)
85
- allow_any_instance_of(::SocialSnippet::CommandLine::Command).to receive(:core).and_return fake_core
86
- end
87
-
88
21
  end
89
-
90
- module SocialSnippet
91
- ::RSpec.configure do |config|
92
- config.include FakeFSHelpers
93
- config.before { enable_fakefs }
94
- config.after { disable_fakefs }
95
-
96
- config.include SpecHelpers
97
- config.before { reset_fake_core }
98
-
99
- config.before(:example, :without_fakefs => true) do
100
- disable_fakefs
101
- make_fake_home
102
- end
103
- end
104
- end
105
-
106
- $WITHOUT_FAKEFS = (ENV["RSPEC_WITHOUT_FAKEFS"] === "true")
@@ -2,12 +2,6 @@ require "spec_helper"
2
2
 
3
3
  describe SocialSnippet::Config, :without_fakefs => $WITHOUT_FAKEFS do
4
4
 
5
- let(:logger) do
6
- logger = ::SocialSnippet::Logger.new(STDOUT)
7
- logger.level = ::SocialSnippet::Logger::Severity::UNKNOWN
8
- logger
9
- end
10
-
11
5
  let(:social_snippet) do
12
6
  class Fake; end
13
7
  Fake.new
@@ -17,7 +11,12 @@ describe SocialSnippet::Config, :without_fakefs => $WITHOUT_FAKEFS do
17
11
  ::SocialSnippet::Config.new(social_snippet)
18
12
  end
19
13
 
20
- before { stub_const "ENV", "HOME" => Dir.mktmpdir }
14
+ before do
15
+ allow(social_snippet).to receive(:storage).and_return fake_storage
16
+ allow(social_snippet).to receive(:logger).and_return fake_logger
17
+ end
18
+
19
+ before { stub_const "ENV", "HOME" => ::Dir.mktmpdir }
21
20
 
22
21
  describe "priority" do
23
22
 
@@ -0,0 +1,201 @@
1
+ require "spec_helper"
2
+
3
+ describe ::SocialSnippet::Repository::Drivers::DriverBase do
4
+
5
+ describe "version" do
6
+
7
+ context "new path/to/repo" do
8
+
9
+ let(:repo) { ::SocialSnippet::Repository::Drivers::DriverBase.new("/path/to/repo") }
10
+
11
+ context "version only cases" do
12
+
13
+ context "has 0.0.1" do
14
+
15
+ before do
16
+ allow(repo).to receive(:refs).and_return([
17
+ '0.0.1',
18
+ ])
19
+ end
20
+
21
+ describe "versions" do
22
+ let(:result) { repo.versions }
23
+ it { expect(result.length).to eq 1 }
24
+ it { expect(result).to include '0.0.1' }
25
+ end # versions
26
+
27
+ describe "latest_version" do
28
+ it { expect(repo.latest_version).to eq '0.0.1' }
29
+ it { expect(repo.latest_version('0')).to eq '0.0.1' }
30
+ it { expect(repo.latest_version('0.0')).to eq '0.0.1' }
31
+ it { expect(repo.latest_version('0.0.1')).to eq '0.0.1' }
32
+ it { expect(repo.latest_version('1.0')).to be_nil }
33
+ end # latest_version
34
+
35
+ end # has 0.0.1
36
+
37
+ context "has 0.0.1, 0.0.2, 0.0.3, 1.0.0" do
38
+
39
+ before do
40
+ allow(repo).to receive(:refs).and_return([
41
+ '0.0.1',
42
+ '0.0.2',
43
+ '0.0.3',
44
+ '1.0.0',
45
+ ])
46
+ end
47
+
48
+ describe "versions" do
49
+
50
+ let(:result) { repo.versions }
51
+
52
+ it { expect(result.length).to eq 4 }
53
+
54
+ context "check result" do
55
+ subject { result }
56
+ it { should include '0.0.1' }
57
+ it { should include '0.0.2' }
58
+ it { should include '0.0.3' }
59
+ it { should include '1.0.0' }
60
+ end
61
+
62
+ end # versions
63
+
64
+ describe "latest_version" do
65
+ it { expect(repo.latest_version).to eq '1.0.0' }
66
+ it { expect(repo.latest_version('0')).to eq '0.0.3' }
67
+ it { expect(repo.latest_version('0.0')).to eq '0.0.3' }
68
+ it { expect(repo.latest_version('1')).to eq '1.0.0' }
69
+ it { expect(repo.latest_version('0.1')).to be_nil }
70
+ end # latest_version
71
+
72
+ end # has 0.0.1, 0.0.2, 0.0.3, 1.0.0
73
+
74
+ context "has 1.2.3, 100.2.300, 123.456.789" do
75
+
76
+ before do
77
+ allow(repo).to receive(:refs).and_return([
78
+ '1.2.3',
79
+ '100.2.300',
80
+ '123.456.789',
81
+ ])
82
+ end
83
+
84
+ describe "versions" do
85
+
86
+ let(:result) { repo.versions }
87
+
88
+ it { expect(result.length).to eq 3 }
89
+
90
+ context "check result" do
91
+ subject { result }
92
+ it { should include '1.2.3' }
93
+ it { should include '100.2.300' }
94
+ it { should include '123.456.789' }
95
+ end
96
+
97
+ end # versions
98
+
99
+ describe "latest_version" do
100
+ it { expect(repo.latest_version).to eq '123.456.789' }
101
+ it { expect(repo.latest_version('0')).to be_nil }
102
+ it { expect(repo.latest_version('0.0')).to be_nil }
103
+ it { expect(repo.latest_version('1')).to eq '1.2.3' }
104
+ it { expect(repo.latest_version('100')).to eq '100.2.300' }
105
+ it { expect(repo.latest_version('100.2')).to eq '100.2.300' }
106
+ it { expect(repo.latest_version('123')).to eq '123.456.789' }
107
+ it { expect(repo.latest_version('123.456')).to eq '123.456.789' }
108
+ end # latest_version
109
+
110
+ end # has 1.2.3, 100.2.300, 123.456.789
111
+
112
+ end # version only cases
113
+
114
+ context "include not version cases" do
115
+
116
+ context "has master, develop, 0.0.1, 0.1.0, 1.0.0" do
117
+
118
+ before do
119
+ allow(repo).to receive(:refs).and_return([
120
+ 'master',
121
+ 'develop',
122
+ '0.0.1',
123
+ '0.1.0',
124
+ '1.0.0',
125
+ ])
126
+ end
127
+
128
+ describe "versions" do
129
+ let(:result) { repo.versions }
130
+
131
+ it { expect(result.length).to eq 3 }
132
+
133
+ context "check result" do
134
+ subject { result }
135
+ it { should include '0.0.1' }
136
+ it { should include '0.1.0' }
137
+ it { should include '1.0.0' }
138
+ end
139
+ end # versions
140
+
141
+ describe "latest_version" do
142
+ it { expect(repo.latest_version).to eq '1.0.0' }
143
+ it { expect(repo.latest_version('0')).to eq '0.1.0' }
144
+ it { expect(repo.latest_version('0.0')).to eq '0.0.1' }
145
+ it { expect(repo.latest_version('1')).to eq '1.0.0' }
146
+ it { expect(repo.latest_version('100')).to be_nil }
147
+ it { expect(repo.latest_version('100.2')).to be_nil }
148
+ it { expect(repo.latest_version('123')).to be_nil}
149
+ it { expect(repo.latest_version('123.456')).to be_nil }
150
+ it { expect(repo.latest_version('master')).to be_nil }
151
+ it { expect(repo.latest_version('develop')).to be_nil }
152
+ end # latest_version
153
+
154
+ end # has master, develop, 0.0.1, 0.1.0, 1.0.0
155
+
156
+ context "has master, feature/0.0.1, 0.0.1/test, 001, 0.0, 1, 1.2.3" do
157
+
158
+ before do
159
+ allow(repo).to receive(:refs).and_return([
160
+ 'master',
161
+ 'feature/0.0.1',
162
+ '0.0.1/test',
163
+ '001',
164
+ '0.0',
165
+ '1',
166
+ '1.2.3',
167
+ ])
168
+ end
169
+
170
+ describe "versions" do
171
+
172
+ let(:result) { repo.versions }
173
+
174
+ it { expect(result.length).to eq 1 }
175
+ it { expect(result).to include '1.2.3' }
176
+
177
+ end # versions
178
+
179
+ describe "latest_version" do
180
+ it { expect(repo.latest_version).to eq '1.2.3' }
181
+ it { expect(repo.latest_version('0')).to be_nil }
182
+ it { expect(repo.latest_version('0.0')).to be_nil }
183
+ it { expect(repo.latest_version('1')).to eq '1.2.3' }
184
+ it { expect(repo.latest_version('100')).to be_nil }
185
+ it { expect(repo.latest_version('100.2')).to be_nil }
186
+ it { expect(repo.latest_version('123')).to be_nil}
187
+ it { expect(repo.latest_version('123.456')).to be_nil }
188
+ it { expect(repo.latest_version('master')).to be_nil }
189
+ it { expect(repo.latest_version('develop')).to be_nil }
190
+ end # latest_version
191
+
192
+ end # has master, feature/0.0.1, 0.0.1/test, 001, 0.0, 1, 1.2.3
193
+
194
+ end # include not version cases
195
+
196
+ end # new path/to/repo
197
+
198
+ end # versions
199
+
200
+ end # ::SocialSnippet::Repository::Drivers::DriverBase
201
+
@@ -0,0 +1,51 @@
1
+ require "spec_helper"
2
+
3
+ describe ::SocialSnippet::Repository::Drivers::GitDriver, :without_fakefs => true do
4
+
5
+ let(:driver) do
6
+ url = "git://github.com/social-snippet/example-repo"
7
+ ::SocialSnippet::Repository::Drivers::GitDriver.new url
8
+ end
9
+
10
+ context "fetch" do
11
+
12
+ before { driver.fetch }
13
+
14
+ context "has versions" do
15
+ subject { driver.has_versions? }
16
+ it { should be_truthy }
17
+ end # has versions
18
+
19
+ context "versions" do
20
+ subject { driver.versions }
21
+ it { should_not include "master" }
22
+ it { should include "1.0.0" }
23
+ it { should include "1.0.1" }
24
+ it { should include "1.0.2" }
25
+ end
26
+
27
+ context "tags" do
28
+ subject { driver.tags }
29
+ it { should_not include "master" }
30
+ it { should include "1.0.0" }
31
+ it { should include "1.0.1" }
32
+ it { should include "1.0.2" }
33
+ end
34
+
35
+ context "refs" do
36
+ subject { driver.refs }
37
+ it { should include "master" }
38
+ it { should include "1.0.0" }
39
+ it { should include "1.0.1" }
40
+ it { should include "1.0.2" }
41
+ end
42
+
43
+ context "snippet_json" do
44
+ subject { driver.snippet_json }
45
+ it { expect(driver.snippet_json["name"]).to eq "example-repo" }
46
+ end
47
+
48
+ end # fetch
49
+
50
+ end # ::SocialSnippet::Repository::Drivers::GitDriver
51
+
@@ -2,55 +2,51 @@ require "spec_helper"
2
2
 
3
3
  describe SocialSnippet::Api::InsertSnippetApi do
4
4
 
5
- before do
6
- allow_any_instance_of(::SocialSnippet::CommandLine::Command).to receive(:social_snippet).and_return fake_core
7
- end
8
-
9
- let(:repo_path) { fake_config.install_path }
10
5
  let(:tmp_repo_path) { "/tmp/repos" }
11
- let(:tmp_repo_path_no_ver) { "/tmp/repos_no_ver" }
12
6
  let(:repo_cache_path) { fake_config.repository_cache_path }
13
- let(:commit_id) { "thisisdummy" }
14
- let(:short_commit_id) { commit_id[0..7] }
15
-
16
- def find_repo_mock
17
- repo_refs = {}
18
- repos = Dir.glob("#{tmp_repo_path}/*").map{|path| Pathname.new(path).basename.to_s }
19
- repos.each do |repo_name|
20
- repo_refs[repo_name] = Dir.glob("#{tmp_repo_path}/#{repo_name}/*").map {|path| Pathname.new(path).basename.to_s }
7
+
8
+ #
9
+ # Helpers
10
+ #
11
+ def reload_tmp_repos
12
+ ::Dir.glob(::File.join tmp_repo_path, "*") do |s|
13
+ repo_name = ::File.basename(s)
14
+ repo = ::SocialSnippet::Repository::Models::Repository.find_or_create_by(
15
+ :name => repo_name,
16
+ )
17
+ add_tmp_repo_refs repo_name, repo
21
18
  end
19
+ end
22
20
 
23
- repos_no_ver = Dir.glob("#{tmp_repo_path_no_ver}/*").map {|path| Pathname.new(path).basename.to_s }
24
-
25
- allow(fake_core.repo_manager).to receive(:find_repository).with(any_args) do |repo_name, ref|
26
- repo_refs[repo_name] ||= []
27
- versions = repo_refs[repo_name].select {|ver| SocialSnippet::Version.is_matched_version_pattern(ref, ver) }
28
- latest_version = VersionSorter.rsort(versions).first
29
-
30
- repo_ref = ref
31
- if repos_no_ver.include?(repo_name)
32
- repo_path = "#{tmp_repo_path_no_ver}/#{repo_name}"
33
- else
34
- base_repo_path = "#{tmp_repo_path}/#{repo_name}/#{repo_refs[repo_name].first}"
35
- base_repo = SocialSnippet::Repository::Drivers::BaseRepository.new(base_repo_path)
36
- allow(base_repo).to receive(:refs).and_return repo_refs[repo_name]
37
- base_repo.load_snippet_json
38
- repo_version = base_repo.latest_version ref
39
- if repo_version.nil?
40
- repo_path = "#{tmp_repo_path}/#{repo_name}/#{repo_ref}"
41
- unless Dir.exists?(repo_path)
42
- raise SocialSnippet::Repository::Errors::NotExistRef
43
- end
44
- else
45
- repo_path = "#{tmp_repo_path}/#{repo_name}/#{repo_version}"
46
- end
47
- end
48
- repo = SocialSnippet::Repository::Drivers::BaseRepository.new(repo_path)
49
- allow(repo).to receive(:refs).and_return repo_refs[repo_name]
50
- allow(repo).to receive(:commit_id).and_return "#{repo_version}#{commit_id}"
51
- repo.load_snippet_json
52
- repo.create_cache repo_cache_path
53
- repo
21
+ def add_tmp_repo_refs(repo_name, repo)
22
+ ::Dir.glob(::File.join tmp_repo_path, repo_name, "*") do |s|
23
+ next unless ::File.directory?(s)
24
+ repo_ref = ::File.basename(s)
25
+ repo.add_ref repo_ref, "rev-#{repo_ref}"
26
+ repo.update_attributes! :current_ref => repo_ref if repo.current_ref.nil?
27
+ repo.add_package repo_ref
28
+ pkg = ::SocialSnippet::Repository::Models::Package.find_or_create_by(
29
+ :repo_name => repo_name,
30
+ :rev_hash => "rev-#{repo_ref}",
31
+ )
32
+ repo_dirpath = ::File.join(tmp_repo_path, repo_name, repo_ref)
33
+ add_tmp_repo_files(repo_dirpath, pkg)
34
+ end
35
+ end
36
+
37
+ def add_tmp_repo_files(repo_dirpath, pkg)
38
+ ::Dir.glob(::File.join repo_dirpath, "**", "*") do |s|
39
+ next unless ::File.file?(s)
40
+ filepath = s[repo_dirpath.length..-1]
41
+ pkg.add_directory ::File.dirname(filepath)
42
+ pkg.add_file filepath, ::File.read(s)
43
+ end
44
+ end
45
+
46
+ def self.prepare_repository(&block)
47
+ before do
48
+ instance_eval &block
49
+ reload_tmp_repos
54
50
  end
55
51
  end
56
52
 
@@ -58,7 +54,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
58
54
 
59
55
  context "use commit id" do
60
56
 
61
- before do
57
+ prepare_repository do
62
58
  repo_name = "my-repo"
63
59
  ref_name = "thisisdu"
64
60
 
@@ -75,11 +71,8 @@ describe SocialSnippet::Api::InsertSnippetApi do
75
71
  ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/file.c", [
76
72
  '/* file.c */'
77
73
  ].join($/)
78
-
79
74
  end # prepare my-repo#thisisdu
80
75
 
81
- before { find_repo_mock }
82
-
83
76
  context "snip my-repo#thisisdu" do
84
77
 
85
78
  let(:input) do
@@ -111,12 +104,12 @@ describe SocialSnippet::Api::InsertSnippetApi do
111
104
  ].join($/)
112
105
  end
113
106
 
114
- it do
115
- expect { fake_core.api.insert_snippet input }.to(
116
- raise_error(SocialSnippet::Repository::Errors::NotExistRef)
117
- )
107
+ subject do
108
+ lambda { fake_core.api.insert_snippet input }
118
109
  end
119
110
 
111
+ it { should raise_error }
112
+
120
113
  end # snip my-repo#thisisdu
121
114
 
122
115
  end # use commid id
@@ -134,38 +127,39 @@ describe SocialSnippet::Api::InsertSnippetApi do
134
127
 
135
128
  context "release 1.0.0" do
136
129
 
137
- before do
130
+ prepare_repository do
138
131
  repo_name = "my-repo"
139
132
  ref_name = "1.0.0"
140
133
 
141
134
  ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}"
142
- ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
143
- ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
144
- ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/func.c"
135
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
136
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
137
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/func.c"
145
138
 
146
- # snippet.json
147
- ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
148
- '{"name": "' + repo_name + '"}'
149
- ].join($/)
139
+ # snippet.json
140
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
141
+ '{"name": "' + repo_name + '"}'
142
+ ].join($/)
150
143
 
151
- ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/func.c", [
152
- 'func: 1.0.0',
153
- ].join($/)
144
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/func.c", [
145
+ 'func: 1.0.0',
146
+ ].join($/)
154
147
  end # prepare my-repo#1.0.0
155
148
 
156
- before { find_repo_mock }
157
-
158
- it do
159
- expect(fake_core.api.insert_snippet(input)).to eq [
149
+ let(:output) do
150
+ [
160
151
  '/* @snippet<my-repo#1.0.0:func.c> */',
161
152
  'func: 1.0.0',
162
153
  'main',
163
- ].join($/).freeze
154
+ ].join($/)
164
155
  end
165
156
 
157
+ subject { fake_core.api.insert_snippet(input) }
158
+ it { should eq output }
159
+
166
160
  context "release 1.0.1" do
167
161
 
168
- before do
162
+ prepare_repository do
169
163
  repo_name = "my-repo"
170
164
  ref_name = "1.0.1"
171
165
 
@@ -184,19 +178,20 @@ describe SocialSnippet::Api::InsertSnippetApi do
184
178
  ].join($/)
185
179
  end # prepare my-repo#1.0.1
186
180
 
187
- before { find_repo_mock }
188
-
189
- it do
190
- expect(fake_core.api.insert_snippet(input)).to eq [
181
+ let(:output) do
182
+ [
191
183
  '/* @snippet<my-repo#1.0.1:func.c> */',
192
184
  'func: 1.0.1',
193
185
  'main',
194
- ].join($/).freeze
186
+ ].join($/)
195
187
  end
196
188
 
189
+ subject { fake_core.api.insert_snippet(input) }
190
+ it { should eq output }
191
+
197
192
  context "release 1.1.0" do
198
193
 
199
- before do
194
+ prepare_repository do
200
195
  repo_name = "my-repo"
201
196
  ref_name = "1.1.0"
202
197
 
@@ -215,8 +210,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
215
210
  ].join($/)
216
211
  end # prepare my-repo#1.1.0
217
212
 
218
- before { find_repo_mock }
219
-
220
213
  it do
221
214
  expect(fake_core.api.insert_snippet(input)).to eq [
222
215
  '/* @snippet<my-repo#1.1.0:func.c> */',
@@ -227,7 +220,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
227
220
 
228
221
  context "release 9.9.9" do
229
222
 
230
- before do
223
+ prepare_repository do
231
224
  repo_name = "my-repo"
232
225
  ref_name = "9.9.9"
233
226
 
@@ -246,8 +239,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
246
239
  ].join($/)
247
240
  end # prepare my-repo#9.9.9
248
241
 
249
- before { find_repo_mock }
250
-
251
242
  it do
252
243
  expect(fake_core.api.insert_snippet(input)).to eq [
253
244
  '/* @snippet<my-repo#1.1.0:func.c> */',
@@ -270,7 +261,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
270
261
 
271
262
  context "use parent path" do
272
263
 
273
- before do
264
+ prepare_repository do
274
265
  repo_name = "my-repo"
275
266
  ref_name = "1.2.3"
276
267
 
@@ -311,8 +302,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
311
302
  ].join($/)
312
303
  end # prepare my-repo#1.2.3
313
304
 
314
- before { find_repo_mock }
315
-
316
305
  context "snip my-repo:file_1" do
317
306
 
318
307
  let(:input) do
@@ -345,7 +334,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
345
334
 
346
335
  context "snip self" do
347
336
 
348
- before do
337
+ prepare_repository do
349
338
  repo_name = "directly"
350
339
  ref_name = "3.2.1"
351
340
 
@@ -366,7 +355,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
366
355
  ].join($/)
367
356
  end # prepare directly#3.2.1
368
357
 
369
- before do
358
+ prepare_repository do
370
359
  repo_name = "loop-1"
371
360
  ref_name = "1.1.1"
372
361
 
@@ -387,7 +376,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
387
376
  ].join($/)
388
377
  end # prepare loop-1#1.1.1
389
378
 
390
- before do
379
+ prepare_repository do
391
380
  repo_name = "loop-2"
392
381
  ref_name = "1.1.1"
393
382
 
@@ -408,7 +397,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
408
397
  ].join($/)
409
398
  end # prepare loop-2#1.1.1
410
399
 
411
- before do
400
+ prepare_repository do
412
401
  repo_name = "loop-3"
413
402
  ref_name = "1.1.1"
414
403
 
@@ -430,7 +419,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
430
419
  ].join($/)
431
420
  end # prepare loop-3#1.1.1
432
421
 
433
- before do
422
+ prepare_repository do
434
423
  repo_name = "non-loop-4"
435
424
  ref_name = "1.1.1"
436
425
 
@@ -450,8 +439,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
450
439
  ].join($/)
451
440
  end # prepare non-loop-4#1.1.1
452
441
 
453
- before { find_repo_mock }
454
-
455
442
  context "indirectly" do
456
443
 
457
444
  context "has cyclic loop" do
@@ -512,7 +499,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
512
499
 
513
500
  context "snippet snippet ... snippet" do
514
501
 
515
- before do
502
+ prepare_repository do
516
503
  repo_name = "my-repo-1"
517
504
  ref_name = "0.0.1"
518
505
 
@@ -533,7 +520,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
533
520
  ].join($/)
534
521
  end # prepare my-repo-1
535
522
 
536
- before do
523
+ prepare_repository do
537
524
  repo_name = "my-repo-2"
538
525
  ref_name = "0.0.1"
539
526
 
@@ -554,7 +541,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
554
541
  ].join($/)
555
542
  end # prepare my-repo-2#0.0.1
556
543
 
557
- before do
544
+ prepare_repository do
558
545
  repo_name = "my-repo-2"
559
546
  ref_name = "1.2.3"
560
547
 
@@ -574,7 +561,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
574
561
  ].join($/)
575
562
  end # prepare my-repo-2#1.2.3
576
563
 
577
- before do
564
+ prepare_repository do
578
565
  repo_name = "my-repo-3"
579
566
  ref_name = "1.2.3"
580
567
 
@@ -597,7 +584,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
597
584
  ].join($/)
598
585
  end # prepare my-repo-3#1.2.3
599
586
 
600
- before do
587
+ prepare_repository do
601
588
  repo_name = "my-repo-4"
602
589
  ref_name = "1.2.3"
603
590
 
@@ -622,7 +609,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
622
609
  ].join($/)
623
610
  end # prepare my-repo-4#1.2.3
624
611
 
625
- before do
612
+ prepare_repository do
626
613
  repo_name = "my-repo-5"
627
614
  ref_name = "100.200.300"
628
615
 
@@ -647,7 +634,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
647
634
  ].join($/)
648
635
  end # prepare my-repo-5#100.200.300
649
636
 
650
- before do
637
+ prepare_repository do
651
638
  repo_name = "my-repo-5"
652
639
  ref_name = "99.999.999"
653
640
 
@@ -672,29 +659,35 @@ describe SocialSnippet::Api::InsertSnippetApi do
672
659
  ].join($/)
673
660
  end # prepare my-repo-5#99.999.999
674
661
 
675
- before do
662
+ prepare_repository do
676
663
  repo_name = "my-repo-6"
664
+ ref_name = "master"
677
665
 
678
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}"
679
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}/.git"
680
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/snippet.json"
681
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}/sources"
682
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}/sources/path"
683
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}/sources/path/to"
684
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/sources/path/to/6"
666
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
667
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
668
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
669
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/sources"
670
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/sources/path"
671
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/sources/path/to"
672
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/sources/path/to/6"
685
673
 
686
674
  # snippet.json
687
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/snippet.json", [
675
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
688
676
  '{"name": "' + repo_name + '", "main": "sources"}'
689
677
  ].join($/)
690
678
 
691
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/sources/path/to/6", [
679
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/sources/path/to/6", [
692
680
  '@snip<my-repo-7:path/to/7>',
693
681
  'my-repo-6:sources/path/to/6',
694
682
  ].join($/)
695
683
  end # prepare my-repo-6
696
684
 
697
685
  before do
686
+ repo = ::SocialSnippet::Repository::Models::Repository.find_by(:name => "my-repo-6")
687
+ repo.update_attributes! :current_ref => "master"
688
+ end
689
+
690
+ prepare_repository do
698
691
  repo_name = "my-repo-7"
699
692
  ref_name = "1.2.3"
700
693
 
@@ -719,8 +712,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
719
712
  ].join($/)
720
713
  end # prepare my-repo-7#1.2.3
721
714
 
722
- before { find_repo_mock }
723
-
724
715
  context "snip my-repo-1:1" do
725
716
 
726
717
  let(:input) do
@@ -734,7 +725,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
734
725
  '/* @snippet <my-repo-7#1.2.3:path/to/7> */',
735
726
  'end',
736
727
  'my-repo-7:sources/path/to/7',
737
- '/* @snippet <my-repo-6#thisisdu:path/to/6> */',
728
+ '/* @snippet <my-repo-6#master:path/to/6> */',
738
729
  'my-repo-6:sources/path/to/6',
739
730
  '/* @snippet <my-repo-5#100.200.300:path/to/5> */',
740
731
  'my-repo-5:sources/path/to/5',
@@ -758,7 +749,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
758
749
 
759
750
  context "snippet snippets" do
760
751
 
761
- before do
752
+ prepare_repository do
762
753
  repo_name = "my-repo"
763
754
  ref_name = "0.0.1"
764
755
 
@@ -826,8 +817,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
826
817
  ].join($/)
827
818
  end
828
819
 
829
- before { find_repo_mock }
830
-
831
820
  let(:input) do
832
821
  [
833
822
  '@snip<my-repo:1>',
@@ -866,7 +855,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
866
855
 
867
856
  context "use multiple repos and multiple versions" do
868
857
 
869
- before do
858
+ prepare_repository do
870
859
  repo_name = "my-repo"
871
860
  ref_name = "0.0.3"
872
861
 
@@ -896,7 +885,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
896
885
 
897
886
  end # prepare my-repo#0.0.3
898
887
 
899
- before do
888
+ prepare_repository do
900
889
  repo_name = "my-repo"
901
890
  ref_name = "0.0.2"
902
891
 
@@ -928,7 +917,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
928
917
 
929
918
  end # prepare my-repo#0.0.2
930
919
 
931
- before do
920
+ prepare_repository do
932
921
  repo_name = "my-repo"
933
922
  ref_name = "0.0.1"
934
923
 
@@ -960,7 +949,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
960
949
 
961
950
  end # prepare my-repo#0.0.1
962
951
 
963
- before do
952
+ prepare_repository do
964
953
  repo_name = "my-repo"
965
954
  ref_name = "1.0.0"
966
955
 
@@ -991,7 +980,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
991
980
  ].join($/)
992
981
  end # prepare my-repo#1.0.0
993
982
 
994
- before do
983
+ prepare_repository do
995
984
  repo_name = "new-my-repo"
996
985
  ref_name = "0.0.1"
997
986
 
@@ -1013,7 +1002,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
1013
1002
  ].join($/)
1014
1003
  end # prepare new-my-repo#0.0.1
1015
1004
 
1016
- before do
1005
+ prepare_repository do
1017
1006
  repo_name = "new-my-repo"
1018
1007
  ref_name = "0.0.2"
1019
1008
 
@@ -1050,10 +1039,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
1050
1039
  ].join($/)
1051
1040
  end # prepare new-my-repo#0.0.2
1052
1041
 
1053
- before do
1054
- find_repo_mock
1055
- end
1056
-
1057
1042
  context "use my-repo" do
1058
1043
 
1059
1044
  let(:input) do
@@ -1163,11 +1148,11 @@ describe SocialSnippet::Api::InsertSnippetApi do
1163
1148
 
1164
1149
  context "use latest version without ref" do
1165
1150
 
1166
- before do
1151
+ prepare_repository do
1167
1152
  repo_name = "my-repo"
1168
1153
  ref_name = "0.0.3"
1169
1154
 
1170
- ::FileUtils.mkdir_p "#{tmp_repo_path}"
1155
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1171
1156
  ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}"
1172
1157
  ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1173
1158
  ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src"
@@ -1197,8 +1182,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
1197
1182
 
1198
1183
  end # prepare my-repo#0.0.3
1199
1184
 
1200
- before { find_repo_mock }
1201
-
1202
1185
  let(:input) do
1203
1186
  [
1204
1187
  '#@snip <my-repo:1.rb>',
@@ -1225,25 +1208,25 @@ describe SocialSnippet::Api::InsertSnippetApi do
1225
1208
 
1226
1209
  context "snippet snippet with ref" do
1227
1210
 
1228
- before do
1211
+ prepare_repository do
1229
1212
  repo_name = "my-repo"
1230
1213
  ref_name = "0.0.1"
1231
1214
 
1232
- ::FileUtils.mkdir_p "#{repo_path}"
1233
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/#{ref_name}"
1234
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/#{ref_name}/.git"
1235
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/#{ref_name}/src"
1236
- ::FileUtils.touch "#{repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1237
- ::FileUtils.touch "#{repo_path}/#{repo_name}/#{ref_name}/src/1.rb"
1238
- ::FileUtils.touch "#{repo_path}/#{repo_name}/#{ref_name}/src/2.rb"
1215
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1216
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}"
1217
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1218
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src"
1219
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1220
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/1.rb"
1221
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/2.rb"
1239
1222
 
1240
1223
  # snippet.json
1241
- ::File.write "#{repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1224
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1242
1225
  '{"name": "' + repo_name + '", "main": "src"}',
1243
1226
  ].join($/)
1244
1227
 
1245
1228
  # src/1.rb
1246
- ::File.write "#{repo_path}/#{repo_name}/#{ref_name}/src/1.rb", [
1229
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/1.rb", [
1247
1230
  '# @snip</2.rb>',
1248
1231
  'def func_1',
1249
1232
  ' return 2 * func_2()',
@@ -1251,45 +1234,33 @@ describe SocialSnippet::Api::InsertSnippetApi do
1251
1234
  ].join($/)
1252
1235
 
1253
1236
  # src/2.rb
1254
- ::File.write "#{repo_path}/#{repo_name}/#{ref_name}/src/2.rb", [
1237
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/2.rb", [
1255
1238
  'def func_2',
1256
1239
  ' return 42',
1257
1240
  'end',
1258
1241
  ].join($/)
1259
1242
 
1260
- allow(fake_core.repo_manager).to receive(:find_repository).with(repo_name, ref_name) do
1261
- repo = SocialSnippet::Repository::Drivers::BaseRepository.new("#{repo_path}/#{repo_name}/#{ref_name}")
1262
- allow(repo).to receive(:refs).and_return([
1263
- '0.0.1',
1264
- '0.0.2',
1265
- ])
1266
- allow(repo).to receive(:commit_id).and_return "#{ref_name}#{commit_id}"
1267
- repo.load_snippet_json
1268
- repo.create_cache repo_cache_path
1269
- repo
1270
- end
1271
-
1272
1243
  end # prepare my-repo#0.0.1
1273
1244
 
1274
- before do
1245
+ prepare_repository do
1275
1246
  repo_name = "my-repo"
1276
1247
  ref_name = "0.0.2"
1277
1248
 
1278
- ::FileUtils.mkdir_p "#{repo_path}"
1279
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/#{ref_name}"
1280
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/#{ref_name}/.git"
1281
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/#{ref_name}/src"
1282
- ::FileUtils.touch "#{repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1283
- ::FileUtils.touch "#{repo_path}/#{repo_name}/#{ref_name}/src/1.rb"
1284
- ::FileUtils.touch "#{repo_path}/#{repo_name}/#{ref_name}/src/2.rb"
1249
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1250
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}"
1251
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1252
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src"
1253
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1254
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/1.rb"
1255
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/2.rb"
1285
1256
 
1286
1257
  # snippet.json
1287
- ::File.write "#{repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1258
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1288
1259
  '{"name": "' + repo_name + '", "main": "src"}',
1289
1260
  ].join($/)
1290
1261
 
1291
1262
  # src/1.rb
1292
- ::File.write "#{repo_path}/#{repo_name}/#{ref_name}/src/1.rb", [
1263
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/1.rb", [
1293
1264
  '# @snip</2.rb>',
1294
1265
  'def func_1',
1295
1266
  ' return 2 * func_2()',
@@ -1297,23 +1268,12 @@ describe SocialSnippet::Api::InsertSnippetApi do
1297
1268
  ].join($/)
1298
1269
 
1299
1270
  # src/2.rb
1300
- ::File.write "#{repo_path}/#{repo_name}/#{ref_name}/src/2.rb", [
1271
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/2.rb", [
1301
1272
  'def func_2',
1302
1273
  ' return 10000 + 42',
1303
1274
  'end',
1304
1275
  ].join($/)
1305
1276
 
1306
- allow(fake_core.repo_manager).to receive(:find_repository).with(repo_name, ref_name) do
1307
- repo = SocialSnippet::Repository::Drivers::BaseRepository.new("#{repo_path}/#{repo_name}/#{ref_name}")
1308
- allow(repo).to receive(:refs).and_return([
1309
- '0.0.1',
1310
- '0.0.2',
1311
- ])
1312
- allow(repo).to receive(:commit_id).and_return "#{ref_name}#{commit_id}"
1313
- repo.load_snippet_json
1314
- repo.create_cache repo_cache_path
1315
- repo
1316
- end
1317
1277
  end # prepare my-repo#0.0.2
1318
1278
 
1319
1279
  context "use 0.0.1" do
@@ -1417,24 +1377,24 @@ describe SocialSnippet::Api::InsertSnippetApi do
1417
1377
 
1418
1378
  context "snippet snippet" do
1419
1379
 
1420
- before do
1380
+ prepare_repository do
1421
1381
  repo_name = "my-repo"
1382
+ ref_name = "master"
1422
1383
 
1423
- ::FileUtils.mkdir_p "#{repo_path}"
1424
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}"
1425
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/.git"
1426
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/src"
1427
- ::FileUtils.touch "#{repo_path}/#{repo_name}/snippet.json"
1428
- ::FileUtils.touch "#{repo_path}/#{repo_name}/src/1.rb"
1429
- ::FileUtils.touch "#{repo_path}/#{repo_name}/src/2.rb"
1384
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1385
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1386
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src"
1387
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1388
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/1.rb"
1389
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/2.rb"
1430
1390
 
1431
1391
  # snippet.json
1432
- ::File.write "#{repo_path}/#{repo_name}/snippet.json", [
1392
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1433
1393
  '{"name": "' + repo_name + '", "main": "src"}',
1434
1394
  ].join($/)
1435
1395
 
1436
1396
  # src/1.rb
1437
- ::File.write "#{repo_path}/#{repo_name}/src/1.rb", [
1397
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/1.rb", [
1438
1398
  '# @snip</2.rb>',
1439
1399
  'def func_1',
1440
1400
  ' return 2 * func_2()',
@@ -1442,24 +1402,12 @@ describe SocialSnippet::Api::InsertSnippetApi do
1442
1402
  ].join($/)
1443
1403
 
1444
1404
  # src/2.rb
1445
- ::File.write "#{repo_path}/#{repo_name}/src/2.rb", [
1405
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/2.rb", [
1446
1406
  'def func_2',
1447
1407
  ' return 42',
1448
1408
  'end',
1449
1409
  ].join($/)
1450
1410
 
1451
- repo_config = Proc.new do |path|
1452
- repo = SocialSnippet::Repository::Drivers::BaseRepository.new("#{repo_path}/#{repo_name}")
1453
- allow(repo).to receive(:commit_id).and_return commit_id
1454
- allow(repo).to receive(:refs).and_return []
1455
- repo.load_snippet_json
1456
- repo.create_cache repo_cache_path
1457
- repo
1458
- end
1459
-
1460
- allow(fake_core.repo_manager).to receive(:find_repository).with(repo_name) { repo_config.call }
1461
- allow(fake_core.repo_manager).to receive(:find_repository).with(repo_name, short_commit_id) { repo_config.call }
1462
-
1463
1411
  end # prepare my-repo
1464
1412
 
1465
1413
  let(:input) do
@@ -1473,11 +1421,11 @@ describe SocialSnippet::Api::InsertSnippetApi do
1473
1421
  let(:output) do
1474
1422
  [
1475
1423
  '# main.rb',
1476
- '# @snippet <my-repo#' + short_commit_id + ':2.rb>',
1424
+ '# @snippet <my-repo#master:2.rb>',
1477
1425
  'def func_2',
1478
1426
  ' return 42',
1479
1427
  'end',
1480
- '# @snippet <my-repo#' + short_commit_id + ':1.rb>',
1428
+ '# @snippet <my-repo#master:1.rb>',
1481
1429
  'def func_1',
1482
1430
  ' return 2 * func_2()',
1483
1431
  'end',
@@ -1492,50 +1440,38 @@ describe SocialSnippet::Api::InsertSnippetApi do
1492
1440
 
1493
1441
  context "snip with repo" do
1494
1442
 
1495
- before do
1443
+ prepare_repository do
1496
1444
  repo_name = "my-repo"
1445
+ ref_name = "master"
1497
1446
 
1498
- ::FileUtils.mkdir_p "#{repo_path}"
1499
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}"
1500
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/.git"
1501
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/a"
1502
- ::FileUtils.touch "#{repo_path}/#{repo_name}/snippet.json"
1503
- ::FileUtils.touch "#{repo_path}/#{repo_name}/a.rb"
1504
- ::FileUtils.touch "#{repo_path}/#{repo_name}/a/1.rb"
1505
- ::FileUtils.touch "#{repo_path}/#{repo_name}/a/2.rb"
1447
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1448
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1449
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/a"
1450
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1451
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/a.rb"
1452
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/a/1.rb"
1453
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/a/2.rb"
1506
1454
 
1507
1455
  # snippet.json
1508
- ::File.write "#{repo_path}/#{repo_name}/snippet.json", [
1456
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1509
1457
  '{"name": "' + repo_name + '"}',
1510
1458
  ].join($/)
1511
1459
 
1512
1460
  # a.rb
1513
- ::File.write "#{repo_path}/#{repo_name}/a.rb", [
1461
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/a.rb", [
1514
1462
  '# @snip <./a/1.rb>',
1515
1463
  '# @snip <./a/2.rb>',
1516
1464
  ].join($/)
1517
1465
 
1518
1466
  # a/1.rb
1519
- ::File.write "#{repo_path}/#{repo_name}/a/1.rb", [
1467
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/a/1.rb", [
1520
1468
  'puts "1"',
1521
1469
  ].join($/)
1522
1470
 
1523
1471
  # a/2.rb
1524
- ::File.write "#{repo_path}/#{repo_name}/a/2.rb", [
1472
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/a/2.rb", [
1525
1473
  'puts "2"',
1526
1474
  ].join($/)
1527
-
1528
- repo_config = Proc.new do |path|
1529
- repo = SocialSnippet::Repository::Drivers::BaseRepository.new("#{repo_path}/#{repo_name}")
1530
- allow(repo).to receive(:commit_id).and_return commit_id
1531
- allow(repo).to receive(:refs).and_return []
1532
- repo.load_snippet_json
1533
- repo.create_cache repo_cache_path
1534
- repo
1535
- end
1536
-
1537
- allow(fake_core.repo_manager).to receive(:find_repository).with("my-repo") { repo_config.call }
1538
- allow(fake_core.repo_manager).to receive(:find_repository).with("my-repo", short_commit_id) { repo_config.call }
1539
1475
  end # my-repo
1540
1476
 
1541
1477
  let(:input) do
@@ -1546,11 +1482,11 @@ describe SocialSnippet::Api::InsertSnippetApi do
1546
1482
 
1547
1483
  let(:output) do
1548
1484
  [
1549
- '# @snippet<my-repo#' + short_commit_id + ':a/1.rb>',
1485
+ '# @snippet<my-repo#master:a/1.rb>',
1550
1486
  'puts "1"',
1551
- '# @snippet<my-repo#' + short_commit_id + ':a/2.rb>',
1487
+ '# @snippet<my-repo#master:a/2.rb>',
1552
1488
  'puts "2"',
1553
- '# @snippet<my-repo#' + short_commit_id + ':a.rb>',
1489
+ '# @snippet<my-repo#master:a.rb>',
1554
1490
  ].join($/)
1555
1491
  end
1556
1492
 
@@ -1561,41 +1497,29 @@ describe SocialSnippet::Api::InsertSnippetApi do
1561
1497
 
1562
1498
  context "multiple snippets without duplicates" do
1563
1499
 
1564
- before do
1500
+ prepare_repository do
1565
1501
  repo_name = "repo-a"
1502
+ ref_name = "master"
1566
1503
 
1567
- ::FileUtils.mkdir_p "#{repo_path}"
1568
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}"
1569
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/.git"
1570
- ::FileUtils.touch "#{repo_path}/#{repo_name}/snippet.json"
1571
- ::FileUtils.touch "#{repo_path}/#{repo_name}/parent"
1572
- ::FileUtils.touch "#{repo_path}/#{repo_name}/child_1"
1573
- ::FileUtils.touch "#{repo_path}/#{repo_name}/child_2"
1574
- ::FileUtils.touch "#{repo_path}/#{repo_name}/child_3"
1504
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1505
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1506
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1507
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/parent"
1508
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/child_1"
1509
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/child_2"
1510
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/child_3"
1575
1511
 
1576
1512
  # snippet.json
1577
- ::File.write "#{repo_path}/#{repo_name}/snippet.json", [
1513
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1578
1514
  '{"name": "' + repo_name + '"}',
1579
1515
  ].join($/)
1580
1516
 
1581
1517
  # parent
1582
- ::File.write "#{repo_path}/#{repo_name}/parent", [
1518
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/parent", [
1583
1519
  '@snip<child_1>',
1584
1520
  '@snip<child_2>',
1585
1521
  '@snip<child_3>',
1586
1522
  ].join($/)
1587
-
1588
- repo_config = Proc.new do |path|
1589
- repo = SocialSnippet::Repository::Drivers::BaseRepository.new("#{repo_path}/#{repo_name}")
1590
- allow(repo).to receive(:commit_id).and_return commit_id
1591
- allow(repo).to receive(:refs).and_return []
1592
- repo.load_snippet_json
1593
- repo.create_cache repo_cache_path
1594
- repo
1595
- end
1596
-
1597
- allow(fake_core.repo_manager).to receive(:find_repository).with(repo_name) { repo_config.call }
1598
- allow(fake_core.repo_manager).to receive(:find_repository).with(repo_name, short_commit_id) { repo_config.call }
1599
1523
  end # repo-a
1600
1524
 
1601
1525
  let(:input) do
@@ -1606,10 +1530,10 @@ describe SocialSnippet::Api::InsertSnippetApi do
1606
1530
 
1607
1531
  let(:output) do
1608
1532
  [
1609
- '@snippet <repo-a#' + short_commit_id + ':child_1>',
1610
- '@snippet <repo-a#' + short_commit_id + ':child_2>',
1611
- '@snippet <repo-a#' + short_commit_id + ':child_3>',
1612
- '@snippet <repo-a#' + short_commit_id + ':parent>',
1533
+ '@snippet <repo-a#master:child_1>',
1534
+ '@snippet <repo-a#master:child_2>',
1535
+ '@snippet <repo-a#master:child_3>',
1536
+ '@snippet <repo-a#master:parent>',
1613
1537
  ].join($/)
1614
1538
  end
1615
1539
 
@@ -1620,25 +1544,25 @@ describe SocialSnippet::Api::InsertSnippetApi do
1620
1544
 
1621
1545
  context "multiple snippets with duplicates" do
1622
1546
 
1623
- before do
1547
+ prepare_repository do
1624
1548
  repo_name = "my_repo"
1549
+ ref_name = "master"
1625
1550
 
1626
- ::FileUtils.mkdir_p "#{repo_path}"
1627
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}"
1628
- ::FileUtils.mkdir_p "#{repo_path}/#{repo_name}/.git"
1629
- ::FileUtils.touch "#{repo_path}/#{repo_name}/snippet.json"
1630
- ::FileUtils.touch "#{repo_path}/#{repo_name}/parent"
1631
- ::FileUtils.touch "#{repo_path}/#{repo_name}/child_1"
1632
- ::FileUtils.touch "#{repo_path}/#{repo_name}/child_2"
1633
- ::FileUtils.touch "#{repo_path}/#{repo_name}/child_3"
1551
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1552
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1553
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1554
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/parent"
1555
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/child_1"
1556
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/child_2"
1557
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/child_3"
1634
1558
 
1635
1559
  # snippet.json
1636
- ::File.write "#{repo_path}/#{repo_name}/snippet.json", [
1560
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1637
1561
  '{"name": "' + repo_name + '"}',
1638
1562
  ].join($/)
1639
1563
 
1640
1564
  # parent
1641
- ::File.write "#{repo_path}/#{repo_name}/parent", [
1565
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/parent", [
1642
1566
  '@snip<child_1>',
1643
1567
  '@snip<child_2>',
1644
1568
  '@snip<child_2>',
@@ -1647,18 +1571,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
1647
1571
  '@snip<child_2>',
1648
1572
  '@snip<child_3>',
1649
1573
  ].join($/)
1650
-
1651
- repo_config = Proc.new do |path|
1652
- repo = SocialSnippet::Repository::Drivers::BaseRepository.new("#{repo_path}/#{repo_name}")
1653
- allow(repo).to receive(:commit_id).and_return commit_id
1654
- allow(repo).to receive(:refs).and_return []
1655
- repo.load_snippet_json
1656
- repo.create_cache repo_cache_path
1657
- repo
1658
- end
1659
-
1660
- allow(fake_core.repo_manager).to receive(:find_repository).with(repo_name) { repo_config.call }
1661
- allow(fake_core.repo_manager).to receive(:find_repository).with(repo_name, short_commit_id) { repo_config.call }
1662
1574
  end
1663
1575
 
1664
1576
  let(:input) do
@@ -1672,10 +1584,10 @@ describe SocialSnippet::Api::InsertSnippetApi do
1672
1584
 
1673
1585
  let(:output) do
1674
1586
  [
1675
- '@snippet <my_repo#' + short_commit_id + ':child_1>',
1676
- '@snippet <my_repo#' + short_commit_id + ':child_2>',
1677
- '@snippet <my_repo#' + short_commit_id + ':child_3>',
1678
- '@snippet <my_repo#' + short_commit_id + ':parent>',
1587
+ '@snippet <my_repo#master:child_1>',
1588
+ '@snippet <my_repo#master:child_2>',
1589
+ '@snippet <my_repo#master:child_3>',
1590
+ '@snippet <my_repo#master:parent>',
1679
1591
  ].join($/)
1680
1592
  end
1681
1593
 
@@ -1688,20 +1600,20 @@ describe SocialSnippet::Api::InsertSnippetApi do
1688
1600
 
1689
1601
  context "already snipped" do
1690
1602
 
1691
- before do
1603
+ prepare_repository do
1692
1604
  repo_name = "my-repo"
1605
+ ref_name = "master"
1693
1606
 
1694
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}"
1695
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}"
1696
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}/.git"
1697
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}/src"
1698
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/snippet.json"
1699
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/src/1"
1700
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/src/2"
1701
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/src/3"
1607
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1608
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1609
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src"
1610
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1611
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/1"
1612
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/2"
1613
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/3"
1702
1614
 
1703
1615
  # snippet.json
1704
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/snippet.json", [
1616
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1705
1617
  '{',
1706
1618
  ' "name": "' + repo_name + '",',
1707
1619
  ' "main": "src"',
@@ -1709,23 +1621,23 @@ describe SocialSnippet::Api::InsertSnippetApi do
1709
1621
  ].join($/)
1710
1622
 
1711
1623
  # src/1
1712
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/src/1", [
1624
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/1", [
1713
1625
  '@snip<2>',
1714
1626
  '@snip<3>',
1715
1627
  ].join($/)
1716
1628
 
1717
1629
  # src/2
1718
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/src/2", [
1630
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/2", [
1719
1631
  '2',
1720
1632
  ].join($/)
1721
1633
 
1722
1634
  # src/3
1723
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/src/3", [
1635
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/3", [
1724
1636
  '3',
1725
1637
  ].join($/)
1726
1638
  end # prepare for my-repo
1727
1639
 
1728
- before do
1640
+ prepare_repository do
1729
1641
  repo_name = "has-version"
1730
1642
  repo_version = "0.0.1"
1731
1643
 
@@ -1765,7 +1677,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
1765
1677
  ].join($/)
1766
1678
  end # prepare has-version#0.0.1
1767
1679
 
1768
- before do
1680
+ prepare_repository do
1769
1681
  repo_name = "has-version"
1770
1682
  repo_version = "1.2.3"
1771
1683
 
@@ -1805,10 +1717,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
1805
1717
  ].join($/)
1806
1718
  end # prepare has-version#1.2.3
1807
1719
 
1808
- before do
1809
- find_repo_mock
1810
- end
1811
-
1812
1720
  context "already snipped using version" do
1813
1721
 
1814
1722
  context "not already snipped" do
@@ -1883,11 +1791,11 @@ describe SocialSnippet::Api::InsertSnippetApi do
1883
1791
  let(:output) do
1884
1792
  [
1885
1793
  '@snippet<has-version#0.0.1:1>',
1886
- '@snippet<my-repo#thisisdu:2>',
1794
+ '@snippet<my-repo#master:2>',
1887
1795
  '2',
1888
- '@snippet<my-repo#thisisdu:3>',
1796
+ '@snippet<my-repo#master:3>',
1889
1797
  '3',
1890
- '@snippet<my-repo#thisisdu:1>',
1798
+ '@snippet<my-repo#master:1>',
1891
1799
  '@snippet<has-version#1.2.3:2>',
1892
1800
  '1.2.3: 2',
1893
1801
  '@snippet<has-version#1.2.3:3>',
@@ -1908,7 +1816,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
1908
1816
 
1909
1817
  let(:input) do
1910
1818
  [
1911
- '@snippet<my-repo#' + short_commit_id + ':3>',
1819
+ '@snippet<my-repo#master:3>',
1912
1820
  '3',
1913
1821
  '',
1914
1822
  '@snip<my-repo:1>',
@@ -1918,12 +1826,12 @@ describe SocialSnippet::Api::InsertSnippetApi do
1918
1826
 
1919
1827
  let(:output) do
1920
1828
  [
1921
- '@snippet<my-repo#' + short_commit_id + ':3>',
1829
+ '@snippet<my-repo#master:3>',
1922
1830
  '3',
1923
1831
  '',
1924
- '@snippet<my-repo#' + short_commit_id + ':2>',
1832
+ '@snippet<my-repo#master:2>',
1925
1833
  '2',
1926
- '@snippet<my-repo#' + short_commit_id + ':1>',
1834
+ '@snippet<my-repo#master:1>',
1927
1835
  ].join($/)
1928
1836
  end
1929
1837
 
@@ -1944,11 +1852,11 @@ describe SocialSnippet::Api::InsertSnippetApi do
1944
1852
 
1945
1853
  let(:output) do
1946
1854
  [
1947
- '@snippet<my-repo#' + short_commit_id + ':2>',
1855
+ '@snippet<my-repo#master:2>',
1948
1856
  '2',
1949
- '@snippet<my-repo#' + short_commit_id + ':3>',
1857
+ '@snippet<my-repo#master:3>',
1950
1858
  '3',
1951
- '@snippet<my-repo#' + short_commit_id + ':1>',
1859
+ '@snippet<my-repo#master:1>',
1952
1860
  ].join($/)
1953
1861
  end
1954
1862
 
@@ -1961,18 +1869,18 @@ describe SocialSnippet::Api::InsertSnippetApi do
1961
1869
 
1962
1870
  context "other repos have same snip tag" do
1963
1871
 
1964
- before do
1872
+ prepare_repository do
1965
1873
  repo_name = "my_lib"
1874
+ ref_name = "master"
1966
1875
 
1967
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}"
1968
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}"
1969
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}/.git"
1970
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}/src/lib"
1971
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/snippet.json"
1972
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/src/lib/add_func.cpp"
1876
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1877
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1878
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/lib"
1879
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1880
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/lib/add_func.cpp"
1973
1881
 
1974
1882
  # snippet.json
1975
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/snippet.json", [
1883
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1976
1884
  '{',
1977
1885
  ' "name": "' + repo_name + '",',
1978
1886
  ' "main": "src"',
@@ -1980,29 +1888,29 @@ describe SocialSnippet::Api::InsertSnippetApi do
1980
1888
  ].join($/)
1981
1889
 
1982
1890
  # src/add_func.cpp
1983
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/src/add_func.cpp", [
1891
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/src/add_func.cpp", [
1984
1892
  'int add_func( int a, int b ) {',
1985
1893
  ' return a + b;',
1986
1894
  '}',
1987
1895
  ].join($/)
1988
1896
  end # prepare for my_lib repo
1989
1897
 
1990
- before do
1898
+ prepare_repository do
1991
1899
  repo_name = "my_repo_a"
1900
+ ref_name = "master"
1992
1901
 
1993
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}"
1994
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}"
1995
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}/.git"
1996
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/snippet.json"
1997
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/use_add_func.cpp"
1902
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1903
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1904
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1905
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/use_add_func.cpp"
1998
1906
 
1999
1907
  # snippet.json
2000
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/snippet.json", [
1908
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
2001
1909
  '{"name": "' + repo_name + '"}',
2002
1910
  ].join($/)
2003
1911
 
2004
1912
  # use_add_func.cpp
2005
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/use_add_func.cpp", [
1913
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/use_add_func.cpp", [
2006
1914
  '// @snip <my_lib:add_func.cpp>',
2007
1915
  'int my_repo_a_use_add_func( int a, int b ) {',
2008
1916
  ' return add_func(a, b);',
@@ -2010,22 +1918,23 @@ describe SocialSnippet::Api::InsertSnippetApi do
2010
1918
  ].join($/)
2011
1919
  end # prepare for my_repo_a repo
2012
1920
 
2013
- before do
1921
+ prepare_repository do
2014
1922
  repo_name = "my_repo_b"
1923
+ ref_name = "master"
2015
1924
 
2016
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}"
2017
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}"
2018
- ::FileUtils.mkdir_p "#{tmp_repo_path_no_ver}/#{repo_name}/.git"
2019
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/snippet.json"
2020
- ::FileUtils.touch "#{tmp_repo_path_no_ver}/#{repo_name}/use_add_func.cpp"
1925
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}"
1926
+ ::FileUtils.mkdir_p "#{tmp_repo_path}/#{repo_name}/#{ref_name}/.git"
1927
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json"
1928
+ ::FileUtils.touch "#{tmp_repo_path}/#{repo_name}/#{ref_name}/use_add_func.cpp"
2021
1929
 
2022
1930
  # snippet.json
2023
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/snippet.json", [
1931
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/snippet.json", [
1932
+
2024
1933
  '{"name": "' + repo_name + '"}',
2025
1934
  ].join($/)
2026
1935
 
2027
1936
  # use_add_func.cpp
2028
- ::File.write "#{tmp_repo_path_no_ver}/#{repo_name}/use_add_func.cpp", [
1937
+ ::File.write "#{tmp_repo_path}/#{repo_name}/#{ref_name}/use_add_func.cpp", [
2029
1938
  '// @snip <my_lib:add_func.cpp>',
2030
1939
  'int my_repo_b_use_add_func( int a, int b ) {',
2031
1940
  ' return add_func(a, b);',
@@ -2033,8 +1942,6 @@ describe SocialSnippet::Api::InsertSnippetApi do
2033
1942
  ].join($/)
2034
1943
  end # prepare for my_repo_b repo
2035
1944
 
2036
- before { find_repo_mock }
2037
-
2038
1945
  let(:input) do
2039
1946
  [
2040
1947
  '#include <iostream>',
@@ -2055,15 +1962,15 @@ describe SocialSnippet::Api::InsertSnippetApi do
2055
1962
  [
2056
1963
  '#include <iostream>',
2057
1964
  '',
2058
- '// @snippet<my_lib#' + short_commit_id + ':add_func.cpp>',
1965
+ '// @snippet<my_lib#master:add_func.cpp>',
2059
1966
  'int add_func( int a, int b ) {',
2060
1967
  ' return a + b;',
2061
1968
  '}',
2062
- '// @snippet<my_repo_a#' + short_commit_id + ':use_add_func.cpp>',
1969
+ '// @snippet<my_repo_a#master:use_add_func.cpp>',
2063
1970
  'int my_repo_a_use_add_func( int a, int b ) {',
2064
1971
  ' return add_func(a, b);',
2065
1972
  '}',
2066
- '// @snippet<my_repo_b#' + short_commit_id + ':use_add_func.cpp>',
1973
+ '// @snippet<my_repo_b#master:use_add_func.cpp>',
2067
1974
  'int my_repo_b_use_add_func( int a, int b ) {',
2068
1975
  ' return add_func(a, b);',
2069
1976
  '}',
@@ -2090,7 +1997,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
2090
1997
 
2091
1998
  context "range cut (simple)" do
2092
1999
 
2093
- before do
2000
+ prepare_repository do
2094
2001
  ::FileUtils.touch "./file1.cpp"
2095
2002
  ::File.write "./file1.cpp", [
2096
2003
  "// @begin_cut",
@@ -2122,7 +2029,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
2122
2029
 
2123
2030
  context "range cut (nested snippet)" do
2124
2031
 
2125
- before do
2032
+ prepare_repository do
2126
2033
  ::FileUtils.touch "./file1.cpp"
2127
2034
  ::File.write "./file1.cpp", [
2128
2035
  "// @begin_cut",
@@ -2165,7 +2072,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
2165
2072
 
2166
2073
  context "cut line" do
2167
2074
 
2168
- before do
2075
+ prepare_repository do
2169
2076
  ::FileUtils.touch "./file1.cpp"
2170
2077
  ::File.write "./file1.cpp", [
2171
2078
  "#include <path/to/lib> // @cut",
@@ -2195,7 +2102,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
2195
2102
 
2196
2103
  context "cut line (nested case)" do
2197
2104
 
2198
- before do
2105
+ prepare_repository do
2199
2106
  ::FileUtils.touch "./file1.cpp"
2200
2107
  ::File.write "./file1.cpp", [
2201
2108
  "#include <path/to/lib> // @cut",
@@ -2238,7 +2145,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
2238
2145
 
2239
2146
  context "Golang Project 1" do
2240
2147
 
2241
- before do
2148
+ prepare_repository do
2242
2149
  ::FileUtils.mkdir "./runner"
2243
2150
  ::FileUtils.touch "./runner/runner.go"
2244
2151
  ::FileUtils.mkdir "./solver"
@@ -2403,7 +2310,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
2403
2310
 
2404
2311
  context "../" do
2405
2312
 
2406
- before do
2313
+ prepare_repository do
2407
2314
  ::FileUtils.mkdir "./foo"
2408
2315
  ::FileUtils.touch "./foo/foo.go"
2409
2316
  ::FileUtils.mkdir "./bar"
@@ -2457,7 +2364,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
2457
2364
 
2458
2365
  context "no_tag is in cut range" do
2459
2366
 
2460
- before do
2367
+ prepare_repository do
2461
2368
  ::FileUtils.touch "proxy.rb"
2462
2369
  ::FileUtils.touch "foo.rb"
2463
2370
  ::FileUtils.mkdir "foo"
@@ -2567,7 +2474,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
2567
2474
 
2568
2475
  context "for ruby module" do
2569
2476
 
2570
- before do
2477
+ prepare_repository do
2571
2478
  ::FileUtils.touch "proxy.rb"
2572
2479
  ::FileUtils.touch "foo.rb"
2573
2480
  ::FileUtils.mkdir "foo"
@@ -2668,7 +2575,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
2668
2575
 
2669
2576
  context "add nested module" do
2670
2577
 
2671
- before do
2578
+ prepare_repository do
2672
2579
  ::FileUtils.touch "foo/bar.rb"
2673
2580
  ::FileUtils.mkdir "foo/bar"
2674
2581
  ::FileUtils.touch "foo/bar/func1.rb"
@@ -2803,7 +2710,7 @@ describe SocialSnippet::Api::InsertSnippetApi do
2803
2710
 
2804
2711
  context "create project on current directory" do
2805
2712
 
2806
- before do
2713
+ prepare_repository do
2807
2714
  ::FileUtils.touch "snippet.c"
2808
2715
  ::FileUtils.mkdir_p "path/to"
2809
2716
  ::FileUtils.touch "path/to/found.c"