social_snippet 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +2 -0
  3. data/.travis.yml +22 -3
  4. data/Gemfile +25 -2
  5. data/Guardfile +9 -0
  6. data/README.md +6 -1
  7. data/Rakefile +51 -1
  8. data/appveyor.yml +36 -0
  9. data/bin/ssnip +10 -0
  10. data/bin/sspm +10 -0
  11. data/lib/social_snippet.rb +15 -4
  12. data/lib/social_snippet/api.rb +238 -0
  13. data/lib/social_snippet/command_line.rb +4 -0
  14. data/lib/social_snippet/command_line/command.rb +158 -0
  15. data/lib/social_snippet/command_line/ssnip.rb +2 -0
  16. data/lib/social_snippet/command_line/ssnip/main_command.rb +26 -0
  17. data/lib/social_snippet/command_line/sspm.rb +3 -0
  18. data/lib/social_snippet/command_line/sspm/main_command.rb +63 -0
  19. data/lib/social_snippet/command_line/sspm/sub_commands.rb +16 -0
  20. data/lib/social_snippet/command_line/sspm/sub_commands/complete_command.rb +28 -0
  21. data/lib/social_snippet/command_line/sspm/sub_commands/info_command.rb +28 -0
  22. data/lib/social_snippet/command_line/sspm/sub_commands/install_command.rb +103 -0
  23. data/lib/social_snippet/command_line/sspm/sub_commands/publish_command.rb +51 -0
  24. data/lib/social_snippet/command_line/sspm/sub_commands/search_command.rb +32 -0
  25. data/lib/social_snippet/command_line/sspm/sub_commands/update_command.rb +37 -0
  26. data/lib/social_snippet/config.rb +92 -0
  27. data/lib/social_snippet/context.rb +89 -0
  28. data/lib/social_snippet/core.rb +40 -0
  29. data/lib/social_snippet/inserter.rb +64 -0
  30. data/lib/social_snippet/logger.rb +9 -0
  31. data/lib/social_snippet/registry.rb +3 -0
  32. data/lib/social_snippet/registry/registry_client.rb +15 -0
  33. data/lib/social_snippet/registry/registry_resources.rb +3 -0
  34. data/lib/social_snippet/registry/registry_resources/base.rb +80 -0
  35. data/lib/social_snippet/registry/registry_resources/repositories.rb +23 -0
  36. data/lib/social_snippet/repository.rb +6 -0
  37. data/lib/social_snippet/repository/drivers.rb +3 -0
  38. data/lib/social_snippet/repository/drivers/base_repository.rb +192 -0
  39. data/lib/social_snippet/repository/drivers/git_repository.rb +76 -0
  40. data/lib/social_snippet/repository/repository_errors.rb +5 -0
  41. data/lib/social_snippet/repository/repository_factory.rb +59 -0
  42. data/lib/social_snippet/repository/repository_installer.rb +86 -0
  43. data/lib/social_snippet/repository/repository_manager.rb +177 -0
  44. data/lib/social_snippet/resolvers.rb +4 -0
  45. data/lib/social_snippet/resolvers/base_resolver.rb +103 -0
  46. data/lib/social_snippet/resolvers/dep_resolver.rb +61 -0
  47. data/lib/social_snippet/resolvers/insert_resolver.rb +100 -0
  48. data/lib/social_snippet/snippet.rb +14 -0
  49. data/lib/social_snippet/tag.rb +198 -0
  50. data/lib/social_snippet/tag_parser.rb +61 -0
  51. data/lib/social_snippet/version.rb +26 -1
  52. data/social_snippet.gemspec +18 -3
  53. data/spec/helpers/codeclimate_helper.rb +4 -0
  54. data/spec/helpers/fakefs_helper.rb +15 -0
  55. data/spec/helpers/webmock_helper.rb +16 -0
  56. data/spec/lib/api_spec.rb +106 -0
  57. data/spec/lib/command_line/sspm_install_spec.rb +224 -0
  58. data/spec/lib/command_line/sspm_search_spec.rb +167 -0
  59. data/spec/lib/command_line/sspm_spec.rb +81 -0
  60. data/spec/lib/config_spec.rb +56 -0
  61. data/spec/lib/context_spec.rb +48 -0
  62. data/spec/lib/core_spec.rb +126 -0
  63. data/spec/lib/inserter_spec.rb +177 -0
  64. data/spec/lib/registry_client_spec.rb +173 -0
  65. data/spec/lib/repository/base_repository_spec.rb +104 -0
  66. data/spec/lib/repository/git_repository_spec.rb +83 -0
  67. data/spec/lib/repository/repository_factory_spec.rb +31 -0
  68. data/spec/lib/repository/repository_installer_spec.rb +63 -0
  69. data/spec/lib/repository/repository_manager_spec.rb +201 -0
  70. data/spec/lib/tag_parser_spec.rb +173 -0
  71. data/spec/lib/tag_spec.rb +93 -0
  72. data/spec/spec_helper.rb +106 -0
  73. data/test/base_repository_test.rb +375 -0
  74. data/test/command_test.rb +39 -0
  75. data/test/context_test.rb +31 -0
  76. data/test/core_test.rb +2091 -0
  77. data/test/git_repository_test.rb +114 -0
  78. data/test/install_command_test.rb +28 -0
  79. data/test/repository_manager_test.rb +109 -0
  80. data/test/tag_parser_test.rb +47 -0
  81. data/test/tag_test.rb +217 -0
  82. data/test/version_test.rb +56 -0
  83. metadata +271 -14
@@ -0,0 +1,104 @@
1
+ require "spec_helper"
2
+
3
+ module SocialSnippet::Repository::Drivers
4
+
5
+ describe BaseRepository do
6
+
7
+ describe "#to_snippet_json" do
8
+
9
+ let(:instance) { BaseRepository.new "/path/to/repo" }
10
+
11
+ before do
12
+ FileUtils.mkdir_p "/path/to/repo"
13
+ FileUtils.mkdir_p "/path/to/repo/my-src"
14
+ FileUtils.touch "/path/to/repo/snippet.json"
15
+
16
+ File.write "/path/to/repo/snippet.json", [
17
+ '{',
18
+ ' "name": "my-name",',
19
+ ' "desc": "my-desc",',
20
+ ' "main": "my-src"',
21
+ '}',
22
+ ].join("\n")
23
+ end # prepare /path/to/repo
24
+
25
+ context "load snippet.json" do
26
+
27
+ before { instance.load_snippet_json }
28
+ let(:result) { JSON.parse(instance.to_snippet_json) }
29
+
30
+ it { expect(result["name"]).to eq "my-name" }
31
+ it { expect(result["desc"]).to eq "my-desc" }
32
+ it { expect(result["main"]).to eq "my-src" }
33
+
34
+ end # load snippet.json
35
+
36
+ end # to_snippet_json
37
+
38
+ describe "#glob" do
39
+
40
+ let(:instance) { BaseRepository.new "/path/to/my-repo" }
41
+
42
+ before do
43
+ FileUtils.mkdir_p "/path/to/my-repo/"
44
+ FileUtils.mkdir_p "/path/to/my-repo/.git/"
45
+ FileUtils.mkdir_p "/path/to/my-repo/src/"
46
+ FileUtils.mkdir_p "/path/to/my-repo/src/sub/"
47
+ FileUtils.touch "/path/to/my-repo/snippet.json"
48
+ FileUtils.touch "/path/to/my-repo/src/file.cpp"
49
+ FileUtils.touch "/path/to/my-repo/src/sub/sub_file_1.cpp"
50
+ FileUtils.touch "/path/to/my-repo/src/sub/sub_file_2.cpp"
51
+ FileUtils.touch "/path/to/my-repo/src/sub/sub_file_3.cpp"
52
+
53
+ File.write "/path/to/my-repo/snippet.json", [
54
+ '{',
55
+ ' "name": "my-name",',
56
+ ' "desc": "my-desc",',
57
+ ' "main": "src"',
58
+ '}',
59
+ ].join("\n")
60
+ end # prepare install_path
61
+
62
+ context "load snippet.json" do
63
+
64
+ before { instance.load_snippet_json }
65
+
66
+ context "glob = file.cpp" do
67
+ subject { instance.glob("file.cpp").map {|file_path| Pathname(file_path).basename.to_s } }
68
+ it { should include "file.cpp" }
69
+ it { should_not include "sub_file_1.cpp" }
70
+ it { should_not include "sub_file_2.cpp" }
71
+ it { should_not include "sub_file_3.cpp" }
72
+ end
73
+
74
+ context "glob = file*" do
75
+ subject { instance.glob("file*").map {|file_path| Pathname(file_path).basename.to_s } }
76
+ it { should include "file.cpp" }
77
+ it { should_not include "sub_file_1.cpp" }
78
+ it { should_not include "sub_file_2.cpp" }
79
+ it { should_not include "sub_file_3.cpp" }
80
+ end
81
+
82
+ context "glob = sub/*" do
83
+ subject { instance.glob("sub/*").map {|file_path| Pathname(file_path).basename.to_s } }
84
+ it { should_not include "file.cpp" }
85
+ it { should include "sub_file_1.cpp" }
86
+ it { should include "sub_file_2.cpp" }
87
+ it { should include "sub_file_3.cpp" }
88
+ end
89
+
90
+ context "glob = sub/sub_file_2.*" do
91
+ subject { instance.glob("sub/sub_file_2.*").map {|file_path| Pathname(file_path).basename.to_s } }
92
+ it { should_not include "file.cpp" }
93
+ it { should_not include "sub_file_1.cpp" }
94
+ it { should include "sub_file_2.cpp" }
95
+ it { should_not include "sub_file_3.cpp" }
96
+ end
97
+
98
+ end # load snippet.json
99
+
100
+ end # glob
101
+
102
+ end # BaseRepository
103
+
104
+ end # SocialSnippet::Repository::Drivers
@@ -0,0 +1,83 @@
1
+ require "spec_helper"
2
+
3
+ module SocialSnippet::Repository::Drivers
4
+
5
+ describe GitRepository, :without_fakefs => true do
6
+
7
+ before { disable_fakefs }
8
+
9
+ context "github repo" do
10
+
11
+ context "clone social-snippet/example-repo" do
12
+
13
+ subject(:repo) { ::SocialSnippet::Repository::RepositoryFactory.clone("git://github.com/social-snippet/example-repo.git") }
14
+
15
+ context "load snippet.json" do
16
+ before { repo.load_snippet_json }
17
+ it { expect(repo.name).to eq "example-repo" }
18
+ end
19
+
20
+ describe "#refs" do
21
+ subject { repo.refs }
22
+ it { should include "master" }
23
+ it { should include "1.0.0" }
24
+ it { should include "1.0.1" }
25
+ end
26
+
27
+ describe "#versions" do
28
+ subject { repo.versions }
29
+ it { should_not include "master" }
30
+ it { should include "1.0.0" }
31
+ it { should include "1.0.1" }
32
+ end
33
+
34
+ context "checkout 1.0.0" do
35
+
36
+ before do
37
+ repo.checkout("1.0.0")
38
+ repo.load_snippet_json
39
+ end
40
+
41
+ it { expect(repo.name).to eq "example-repo" }
42
+
43
+ describe "#commit_id" do
44
+ subject { repo.commit_id }
45
+ it { should eq "efa58ecae07cf3d063ae75fa97fce164c56d205a" }
46
+ end
47
+
48
+ describe "#short_commit_id" do
49
+ subject { repo.short_commit_id }
50
+ it { should eq "efa58eca" }
51
+ end
52
+
53
+ end # checkout 1.0.0
54
+
55
+ context "checkout 1.0.x (latest)" do
56
+
57
+ before do
58
+ repo.checkout(repo.latest_version("1.0"))
59
+ repo.load_snippet_json
60
+ end
61
+
62
+ it { expect(repo.name).to eq "example-repo" }
63
+
64
+ describe "#commit_id" do
65
+ subject { repo.commit_id }
66
+ it { should eq "073f4411f5251745b339d57356e2560f386e268c" }
67
+ end
68
+
69
+ describe "#short_commit_id" do
70
+ subject { repo.short_commit_id }
71
+ it { should eq "073f4411" }
72
+ end
73
+
74
+ end # checkout 1.0.x
75
+
76
+ end # clone social_snippet/example-repo
77
+
78
+ end # github repo
79
+
80
+ end # GitRepository
81
+
82
+ end # SocialSnippet::Repository::Drivers
83
+
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ module SocialSnippet::Repository
4
+
5
+ describe RepositoryFactory do
6
+
7
+ describe "#is_git_repo" do
8
+
9
+ context "git protocol" do
10
+ context "git://url/to/repo" do
11
+ it { expect(RepositoryFactory.is_git_repo(URI.parse("git://url/to/repo"))).to be_truthy }
12
+ end
13
+ end # git protocol
14
+
15
+ context "github.com" do
16
+ context "git://" do
17
+ it { expect(RepositoryFactory.is_git_repo(URI.parse("git://github.com/user/repo"))).to be_truthy }
18
+ end
19
+ context "http://" do
20
+ it { expect(RepositoryFactory.is_git_repo(URI.parse("http://github.com/user/repo"))).to be_truthy }
21
+ end
22
+ context "https://" do
23
+ it { expect(RepositoryFactory.is_git_repo(URI.parse("https://github.com/user/repo"))).to be_truthy }
24
+ end
25
+ end # github.com
26
+
27
+ end # is_git_repo
28
+
29
+ end # RepositoryFactory
30
+
31
+ end # SocialSnippet::Repository
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ describe ::SocialSnippet::Repository::RepositoryInstaller do
4
+
5
+ let(:config) { ::SocialSnippet::Config.new(social_snippet) }
6
+
7
+ let(:social_snippet) do
8
+ class Fake; end
9
+ Fake.new
10
+ end
11
+
12
+ before do
13
+ allow(social_snippet).to receive(:config).and_return config
14
+ end
15
+
16
+ let(:installer) { ::SocialSnippet::Repository::RepositoryInstaller.new social_snippet }
17
+
18
+
19
+ describe "#each()" do
20
+
21
+ context "add four repos" do
22
+
23
+ before do
24
+ installer.add "my-repo-1", "0.0.1"
25
+ installer.add "my-repo-2", "0.0.1"
26
+ installer.add "my-repo-2", "0.0.2"
27
+ installer.add "my-repo-3", "0.0.1"
28
+ end # prepare for repos
29
+
30
+ context "call each()" do
31
+ it { expect {|b| installer.each &b }.to yield_successive_args("my-repo-1", "my-repo-2", "my-repo-3") }
32
+ end
33
+
34
+ end # add four repos
35
+
36
+ end # each
37
+
38
+
39
+ context "add my-repo#0.0.1" do
40
+
41
+ before { installer.add "my-repo", "0.0.1" }
42
+
43
+ context "installed my-repo#0.0.1" do
44
+ subject { installer.exists? "my-repo", "0.0.1" }
45
+ it { should be_truthy }
46
+
47
+ context "remove my-repo#0.0.1" do
48
+ before { installer.remove "my-repo", "0.0.1" }
49
+
50
+ context "not installed my-repo#0.0.1" do
51
+
52
+ subject { installer.exists? "my-repo", "0.0.1" }
53
+ it { should be_falsey }
54
+
55
+ end # not installed my-repo#0.0.1
56
+
57
+ end # remove my-repo#0.0.1
58
+
59
+ end # installed my-repo#0.0.1
60
+
61
+ end # add my-repo#0.0.1
62
+
63
+ end # ::SocialSnippet::Repository::RepositoryInstaller
@@ -0,0 +1,201 @@
1
+ require "spec_helper"
2
+
3
+ module SocialSnippet::Repository
4
+
5
+ describe RepositoryManager, :repository_manager_current => true do
6
+
7
+ before { stub_const "ENV", "SOCIAL_SNIPPET_HOME" => "/path/to" }
8
+
9
+ let(:rest_resource) { ::RestClient::Resource.new "http://api.server/api/dummy" }
10
+
11
+ before do
12
+ allow_any_instance_of(::SocialSnippet::Registry::RegistryResources::Base).to receive(:rest_client) do
13
+ rest_resource
14
+ end
15
+ end # use dummy api server
16
+
17
+ let(:logger) do
18
+ logger = ::SocialSnippet::Logger.new(STDOUT)
19
+ logger.level = ::SocialSnippet::Logger::Severity::UNKNOWN
20
+ logger
21
+ end
22
+
23
+ let(:config) do
24
+ ::SocialSnippet::Config.new(social_snippet)
25
+ end
26
+
27
+ let(:social_snippet) do
28
+ class Fake; end
29
+ Fake.new
30
+ end
31
+
32
+ before do
33
+ allow(social_snippet).to receive(:logger).and_return logger
34
+ allow(social_snippet).to receive(:config).and_return config
35
+ end
36
+
37
+ let(:repo_manager) { RepositoryManager.new social_snippet }
38
+ let(:commit_id) { "dummycommitid" }
39
+ let(:short_commit_id) { commit_id[0..7] }
40
+
41
+ describe "#resolve_snippet_path" do
42
+
43
+ context "without repo" do
44
+
45
+ context "cur = path/to/file.cpp" do
46
+
47
+ let(:context) { ::SocialSnippet::Context.new("path/to/file.cpp") }
48
+
49
+ context "@snip<./file2.cpp>" do
50
+
51
+ let(:tag) { ::SocialSnippet::Tag.new("// @snip<./file2.cpp>") }
52
+
53
+ context "result" do
54
+ subject { repo_manager.resolve_snippet_path(context, tag) }
55
+ it { should eq "path/to/file2.cpp" }
56
+ end
57
+
58
+ end # snip<./file2.cpp>
59
+
60
+ context "@snip <./subdir/file3.cpp>" do
61
+
62
+ let(:tag) { ::SocialSnippet::Tag.new("// @snip <./subdir/file3.cpp>") }
63
+
64
+ context "result" do
65
+ subject { repo_manager.resolve_snippet_path(context, tag) }
66
+ it { should eq "path/to/subdir/file3.cpp" }
67
+ end
68
+
69
+ end # snip <./subdir/file3.cpp>
70
+
71
+ end # cur = path/to/file.cpp
72
+
73
+ end # without repo
74
+
75
+ context "with repo" do
76
+
77
+ let(:repo_path) { "#{ENV["SOCIAL_SNIPPET_HOME"]}/repo" }
78
+
79
+ before do
80
+ FileUtils.mkdir_p "#{repo_path}/repo_a"
81
+ FileUtils.mkdir_p "#{repo_path}/repo_a/.git"
82
+ FileUtils.touch "#{repo_path}/repo_a/snippet.json"
83
+
84
+ File.write "#{repo_path}/repo_a/snippet.json", [
85
+ '{',
86
+ ' "name": "repo_a",',
87
+ ' "desc": "this is repo_a",',
88
+ ' "language": "C++",',
89
+ ' "main": "src"',
90
+ '}',
91
+ ].join("\n")
92
+
93
+ allow(repo_manager).to receive(:find_repository).with("repo_a") do |path|
94
+ repo = ::SocialSnippet::Repository::Drivers::BaseRepository.new("#{repo_path}/repo_a")
95
+ expect(repo).to receive(:commit_id).and_return commit_id
96
+ repo.load_snippet_json
97
+ repo.create_cache repo_manager.repo_cache_path
98
+ repo
99
+ end
100
+ end
101
+
102
+ context "cur = path/to/file.cpp" do
103
+
104
+ let(:context) { ::SocialSnippet::Context.new("path/to/file.cpp") }
105
+
106
+ context "@snip<repo_a:path/to/file2.cpp>" do
107
+
108
+ let(:tag) { ::SocialSnippet::Tag.new("// @snip<repo_a:path/to/file2.cpp>") }
109
+
110
+ context "result" do
111
+ subject { repo_manager.resolve_snippet_path(context, tag) }
112
+ it { should eq "/path/to/repo_cache/repo_a/#{commit_id[0..7]}/src/path/to/file2.cpp" }
113
+ end
114
+
115
+ end # snip<./file2.cpp>
116
+
117
+ end # cur = path/to/file.cpp
118
+
119
+ end # with repo
120
+
121
+ end # resolve_snippet_path
122
+
123
+ describe "#find_repository" do
124
+
125
+ let(:repo_path) { "#{ENV["SOCIAL_SNIPPET_HOME"]}/repo" }
126
+
127
+ context "create repo_a as a git repo" do
128
+
129
+ before do
130
+ FileUtils.mkdir_p "#{repo_path}/repo_a"
131
+ FileUtils.mkdir_p "#{repo_path}/repo_a/.git"
132
+ FileUtils.touch "#{repo_path}/repo_a/snippet.json"
133
+
134
+ File.write "#{repo_path}/repo_a/snippet.json", [
135
+ '{',
136
+ ' "name": "repo_a",',
137
+ ' "desc": "this is repo_a",',
138
+ ' "language": "C++"',
139
+ '}',
140
+ ].join("\n")
141
+ end
142
+
143
+ before do
144
+ expect(::SocialSnippet::Repository::Drivers::GitRepository).to receive(:new) do |path|
145
+ ::SocialSnippet::Repository::Drivers::BaseRepository.new(path)
146
+ end
147
+ expect_any_instance_of(::SocialSnippet::Repository::Drivers::BaseRepository).to receive(:commit_id).and_return commit_id
148
+ end
149
+
150
+ context "find repo_a" do
151
+ let(:repo) { repo_manager.find_repository("repo_a") }
152
+ it { expect(repo.name).to eq "repo_a" }
153
+ it { expect(repo.desc).to eq "this is repo_a" }
154
+ end # find repo_a
155
+
156
+ end # create three repos
157
+
158
+ end # find_repository
159
+
160
+ describe "find_repositories_start_with" do
161
+
162
+ let(:dummy_install_path) { "/path/to/install/path" }
163
+
164
+ before do
165
+ FileUtils.mkdir_p "#{dummy_install_path}"
166
+ FileUtils.mkdir_p "#{dummy_install_path}/my-repo"
167
+ FileUtils.mkdir_p "#{dummy_install_path}/new-repo"
168
+ FileUtils.mkdir_p "#{dummy_install_path}/my-graph-lib"
169
+ FileUtils.mkdir_p "#{dummy_install_path}/my-math-lib"
170
+ allow(repo_manager.installer).to receive(:path).and_return dummy_install_path
171
+ end # prepare install_path
172
+
173
+ context "find my-" do
174
+ subject { repo_manager.find_repositories_start_with("my-") }
175
+ it { should include "my-repo" }
176
+ it { should_not include "new-repo" }
177
+ it { should include "my-graph-lib" }
178
+ it { should include "my-math-lib" }
179
+ end
180
+
181
+ context "find my-re" do
182
+ subject { repo_manager.find_repositories_start_with("my-re") }
183
+ it { should include "my-repo" }
184
+ it { should_not include "new-repo" }
185
+ it { should_not include "my-graph-lib" }
186
+ it { should_not include "my-math-lib" }
187
+ end
188
+
189
+ context "find new-" do
190
+ subject { repo_manager.find_repositories_start_with("new-") }
191
+ it { should include "new-repo" }
192
+ it { should_not include "my-repo" }
193
+ it { should_not include "my-graph-lib" }
194
+ it { should_not include "my-math-lib" }
195
+ end
196
+
197
+ end # find_repositories_start_with
198
+
199
+ end # RepositoryManager
200
+
201
+ end # SocialSnippet::Repository