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,56 @@
1
+ require "spec_helper"
2
+
3
+ describe SocialSnippet::Config, :without_fakefs => $WITHOUT_FAKEFS do
4
+
5
+ let(:logger) do
6
+ logger = ::SocialSnippet::Logger.new(STDOUT)
7
+ logger.level = ::SocialSnippet::Logger::Severity::UNKNOWN
8
+ logger
9
+ end
10
+
11
+ let(:config) do
12
+ ::SocialSnippet::Config.new(social_snippet)
13
+ end
14
+
15
+ let(:social_snippet) do
16
+ class Fake; end
17
+ Fake.new
18
+ end
19
+
20
+ describe "#new" do
21
+
22
+ before { stub_const "ENV", "HOME" => Dir.mktmpdir }
23
+
24
+ context "use default value" do
25
+
26
+ let(:config) do
27
+ ::SocialSnippet::Config.new(social_snippet)
28
+ end
29
+
30
+ context "#home" do
31
+ subject { config.home }
32
+ it { should eq "#{ENV["HOME"]}/.social-snippet" }
33
+ end
34
+
35
+ end # use default value
36
+
37
+ context "set ENV[SOCIAL_SNIPPET_HOME]" do
38
+
39
+ before { stub_const "ENV", "SOCIAL_SNIPPET_HOME" => Dir.mktmpdir }
40
+
41
+ let(:config) do
42
+ ::SocialSnippet::Config.new(social_snippet)
43
+ end
44
+
45
+ context "#home" do
46
+ subject { config.home }
47
+ it { should_not eq "#{ENV["HOME"]}/.social-snippet" }
48
+ it { should eq ENV["SOCIAL_SNIPPET_HOME"] }
49
+ end
50
+
51
+ end # set ENV[SOCIAL_SNIPPET_HOME]
52
+
53
+ end # new
54
+
55
+ end # SocialSnippet::Config
56
+
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe SocialSnippet::Context do
4
+
5
+ let(:context) { SocialSnippet::Context.new("path/to/file.cpp") }
6
+
7
+ describe "#move" do
8
+
9
+ context "move ./file2.cpp" do
10
+
11
+ before { context.move "./file2.cpp" }
12
+
13
+ it { expect(context.path).to eq "path/to/file2.cpp" }
14
+ it { expect(context.repo).to be_nil }
15
+
16
+ context "move path/to/file.cpp, repo" do
17
+
18
+ before { context.move "path/to/file.cpp", "repo" }
19
+
20
+ it { expect(context.path).to eq "path/to/file.cpp" }
21
+ it { expect(context.repo).to eq "repo" }
22
+
23
+ context "move subdir/file.cpp" do
24
+
25
+ before { context.move "subdir/file.cpp" }
26
+
27
+ it { expect(context.path).to eq "path/to/subdir/file.cpp" }
28
+ it { expect(context.repo).to eq "repo" }
29
+
30
+ end # move subdir/file.cpp
31
+
32
+ end # move path/to/file.cpp, repo
33
+
34
+ context "move subdir/file.cpp" do
35
+
36
+ before { context.move "subdir/file.cpp" }
37
+
38
+ it { expect(context.path).to eq "path/to/subdir/file.cpp" }
39
+ it { expect(context.repo).to be_nil }
40
+
41
+ end # move subdir/file.cpp
42
+
43
+ end # move file2.cpp
44
+
45
+ end # move
46
+
47
+ end # SocialSnippet::Context
48
+
@@ -0,0 +1,126 @@
1
+ require "spec_helper"
2
+
3
+ module SocialSnippet
4
+
5
+ describe Core do
6
+
7
+ let(:commit_id) { "dummycommitid" }
8
+ let(:short_commit_id) { commit_id[0..7] }
9
+ let(:repo_path) { "#{ENV["HOME"]}/.social-snippet/repo" }
10
+ let(:repo_cache_path) { "#{ENV["HOME"]}/.social-snippet/repo_cache" }
11
+
12
+ describe "#insert_snippet" do
13
+
14
+ context "create files" do
15
+
16
+ before do
17
+ repo_name = "my-repo"
18
+
19
+ FileUtils.mkdir_p "#{repo_path}"
20
+ FileUtils.mkdir_p "#{repo_path}/my-repo"
21
+ FileUtils.mkdir_p "#{repo_path}/my-repo/.git"
22
+ FileUtils.mkdir_p "#{repo_path}/my-repo/src"
23
+ FileUtils.touch "#{repo_path}/my-repo/snippet.json"
24
+ FileUtils.touch "#{repo_path}/my-repo/src/get_42.cpp"
25
+
26
+ # snippet.json
27
+ File.write "#{repo_path}/my-repo/snippet.json", [
28
+ '{',
29
+ ' "name": "my-repo",',
30
+ ' "language": "C++",',
31
+ ' "main": "src/"',
32
+ '}',
33
+ ].join("\n")
34
+
35
+ # src/get_42.cpp
36
+ File.write "#{repo_path}/my-repo/src/get_42.cpp", [
37
+ 'int get_42() {',
38
+ ' return 42;',
39
+ '}',
40
+ ].join("\n")
41
+
42
+ repo_config = Proc.new do |path|
43
+ repo = ::SocialSnippet::Repository::Drivers::BaseRepository.new("#{repo_path}/my-repo")
44
+ allow(repo).to receive(:commit_id).and_return commit_id
45
+ allow(repo).to receive(:refs).and_return []
46
+ repo.load_snippet_json
47
+ repo.create_cache repo_cache_path
48
+ repo
49
+ end
50
+
51
+ allow(fake_social_snippet.repo_manager).to receive(:find_repository).with("my-repo") { repo_config.call }
52
+ allow(fake_social_snippet.repo_manager).to receive(:find_repository).with("my-repo", short_commit_id) { repo_config.call }
53
+ end # prepare for my-repo
54
+
55
+ context "there are no @snip tags" do
56
+
57
+ let(:input) do
58
+ [
59
+ '#include <iostream>',
60
+ '',
61
+ 'int main() {',
62
+ ' std::cout << get_42() << std::endl;',
63
+ ' return 0;',
64
+ '}',
65
+ ].join("\n")
66
+ end
67
+
68
+ let(:output) do
69
+ [
70
+ '#include <iostream>',
71
+ '',
72
+ 'int main() {',
73
+ ' std::cout << get_42() << std::endl;',
74
+ ' return 0;',
75
+ '}',
76
+ ].join("\n")
77
+ end
78
+
79
+ subject { fake_social_snippet.api.insert_snippet(input) }
80
+ it { should eq output }
81
+
82
+ end # there is no @snip tags
83
+
84
+ context "there is a @snip tag" do
85
+
86
+ let(:input) do
87
+ [
88
+ '#include <iostream>',
89
+ '',
90
+ '// @snip <my-repo:get_42.cpp>',
91
+ '',
92
+ 'int main() {',
93
+ ' std::cout << get_42() << std::endl;',
94
+ ' return 0;',
95
+ '}',
96
+ ].join("\n")
97
+ end
98
+
99
+ let(:output) do
100
+ [
101
+ '#include <iostream>',
102
+ '',
103
+ '// @snippet <my-repo#dummycom:get_42.cpp>',
104
+ 'int get_42() {',
105
+ ' return 42;',
106
+ '}',
107
+ '',
108
+ 'int main() {',
109
+ ' std::cout << get_42() << std::endl;',
110
+ ' return 0;',
111
+ '}',
112
+ ].join("\n")
113
+ end
114
+
115
+ subject { fake_social_snippet.api.insert_snippet(input) }
116
+ it { should eq output }
117
+
118
+ end # there is a @snip tag
119
+
120
+ end # create file
121
+
122
+ end # insert_snippet
123
+
124
+ end # SocialSnippet::Core
125
+
126
+ end # SocialSnippet
@@ -0,0 +1,177 @@
1
+ require "spec_helper"
2
+
3
+ module SocialSnippet
4
+
5
+ describe Inserter do
6
+
7
+ context "new include, snip" do
8
+
9
+ let(:instance) do
10
+ Inserter.new [
11
+ '#include <iostream>',
12
+ '',
13
+ '// @snip<my-repo:path/to/func.cpp>',
14
+ ]
15
+ end
16
+
17
+ context "set_index 2" do
18
+
19
+ before { instance.set_index 2 }
20
+
21
+ context "ignore" do
22
+
23
+ before { instance.ignore }
24
+
25
+ let(:output) do
26
+ [
27
+ '#include <iostream>',
28
+ '',
29
+ ].join("\n").freeze
30
+ end
31
+
32
+ it { expect(instance.to_s).to eq output }
33
+
34
+ context "insert snippet" do
35
+
36
+ before do
37
+ instance.insert [
38
+ '// @snippet<my-repo:path/to/func.cpp>',
39
+ ].freeze
40
+ end
41
+
42
+ let(:output) do
43
+ [
44
+ '#include <iostream>',
45
+ '',
46
+ '// @snippet<my-repo:path/to/func.cpp>',
47
+ ].join("\n").freeze
48
+ end
49
+
50
+ it { expect(instance.to_s).to eq output }
51
+
52
+ context "insert code, code, code" do
53
+
54
+ before do
55
+ instance.insert [
56
+ 'code',
57
+ 'code',
58
+ 'code',
59
+ ].freeze
60
+ end
61
+
62
+ let(:output) do
63
+ [
64
+ '#include <iostream>',
65
+ '',
66
+ '// @snippet<my-repo:path/to/func.cpp>',
67
+ 'code',
68
+ 'code',
69
+ 'code',
70
+ ].join("\n").freeze
71
+ end
72
+
73
+ it { expect(instance.to_s).to eq output }
74
+
75
+ end # insert code, code, code
76
+
77
+ end # insert snippet
78
+
79
+ end # ignore
80
+
81
+ end # set_index 2
82
+
83
+ end # new include, snip
84
+
85
+ context "from empty" do
86
+
87
+ let(:instance) { Inserter.new([]) }
88
+
89
+ context "insert 1, 2, 3" do
90
+
91
+ before do
92
+ instance.insert [
93
+ '1',
94
+ '2',
95
+ '3',
96
+ ].freeze
97
+ end
98
+
99
+ let(:output) do
100
+ [
101
+ '1',
102
+ '2',
103
+ '3',
104
+ ].join("\n").freeze
105
+ end
106
+
107
+ it { expect(instance.to_s).to eq output }
108
+
109
+ context "ignore" do
110
+
111
+ before { instance.ignore }
112
+
113
+ let(:output) do
114
+ [
115
+ '1',
116
+ '2',
117
+ '3',
118
+ ].join("\n").freeze
119
+ end
120
+
121
+ it { expect(instance.to_s).to eq output }
122
+
123
+ context "insert a" do
124
+
125
+ before do
126
+ instance.insert [
127
+ 'a',
128
+ ].freeze
129
+ end
130
+
131
+ let(:output) do
132
+ [
133
+ '1',
134
+ '2',
135
+ '3',
136
+ 'a',
137
+ ].join("\n").freeze
138
+ end
139
+
140
+ it { expect(instance.to_s).to eq output }
141
+
142
+ end # insert a
143
+
144
+ end # ignore
145
+
146
+ end # insert 1, 2, 3
147
+
148
+ context "insert AAA" do
149
+
150
+ before { instance.insert ['AAA'] }
151
+ subject { instance.to_s }
152
+ it { should eq "AAA" }
153
+
154
+ context "insert BBB, CCC" do
155
+
156
+ before { instance.insert ['BBB', 'CCC'] }
157
+ subject { instance.to_s }
158
+
159
+ let(:output) do
160
+ [
161
+ 'AAA',
162
+ 'BBB',
163
+ 'CCC',
164
+ ].join("\n")
165
+ end
166
+
167
+ it { should eq output }
168
+
169
+ end # insert BBB, CCC
170
+
171
+ end # insert AAA
172
+
173
+ end # empty
174
+
175
+ end # Inserter
176
+
177
+ end # SocialSnippet
@@ -0,0 +1,173 @@
1
+ require "spec_helper"
2
+
3
+ module SocialSnippet
4
+
5
+ describe Registry::RegistryClient do
6
+
7
+ let(:fake_repos) do
8
+ [
9
+ {
10
+ "name" => "my-repo",
11
+ "desc" => "This is my repository.",
12
+ },
13
+ {
14
+ "name" => "new-repo",
15
+ "desc" => "This is my repository.",
16
+ },
17
+ ]
18
+ end
19
+
20
+ let(:config) do
21
+ config = Config.new(
22
+ social_snippet,
23
+ {
24
+ :sspm_host => "api.server",
25
+ :sspm_version => "v0",
26
+ :sspm_protocol => "http",
27
+ }
28
+ )
29
+ end
30
+
31
+ def logger
32
+ ::Logger.new(::StringIO.new)
33
+ end
34
+
35
+ let(:social_snippet) do
36
+ class Fake; end
37
+ Fake.new
38
+ end
39
+
40
+ before do
41
+ allow(social_snippet).to receive(:config).and_return config
42
+ allow(social_snippet).to receive(:logger).and_return logger
43
+ end
44
+
45
+ before do
46
+ WebMock
47
+ .stub_request(
48
+ :post,
49
+ "http://api.server/api/v0/repositories",
50
+ )
51
+ .with(
52
+ :headers => {
53
+ "X-CSRF-TOKEN" => "fake-token",
54
+ },
55
+ )
56
+ .to_return(
57
+ :status => 200,
58
+ :body => "ok",
59
+ :headers => {
60
+ "Content-Type" => "plain/text",
61
+ },
62
+ )
63
+ end # POST /api/v0/repositories
64
+
65
+ before do
66
+ WebMock
67
+ .stub_request(
68
+ :get,
69
+ "http://api.server/api/v0/token",
70
+ )
71
+ .to_return(
72
+ :status => 200,
73
+ :body => "fake-token",
74
+ :headers => {
75
+ "Content-Type" => "plain/text",
76
+ },
77
+ )
78
+ end # GET /api/v0/token
79
+
80
+ before do
81
+ WebMock
82
+ .stub_request(
83
+ :get,
84
+ "http://api.server/api/v0/repositories",
85
+ )
86
+ .to_return(
87
+ :status => 200,
88
+ :body => fake_repos.to_json,
89
+ :headers => {
90
+ "Content-Type" => "application/json",
91
+ },
92
+ )
93
+ end # GET /api/v0/repositories
94
+
95
+ before do
96
+ WebMock
97
+ .stub_request(
98
+ :get,
99
+ /http:\/\/api\.server\/api\/v0\/repositories\?q=/
100
+ )
101
+ .to_return do |req|
102
+ params = CGI.parse(req.uri.query)
103
+ {
104
+ :status => 200,
105
+ :body => fake_repos.select {|repo|
106
+ /#{params["q"][0]}/ === repo["name"]
107
+ }.to_json,
108
+ :headers => {
109
+ "Content-Type" => "application/json",
110
+ },
111
+ }
112
+ end
113
+ end # GET /api/v0/repositories?q=...
114
+
115
+ context "create registry_client" do
116
+
117
+ let(:registry_client) { Registry::RegistryClient.new(social_snippet) }
118
+
119
+ context "add repository" do
120
+
121
+ let(:result) { registry_client.repositories.add_url "git://github.com/user/repo" }
122
+ it { expect(result).to eq "ok" }
123
+
124
+ end # add repository
125
+
126
+ context "repositories" do
127
+
128
+ let(:result) { registry_client.repositories.all }
129
+
130
+ context "check result" do
131
+ let(:result_names) { result.map {|repo| repo["name"] } }
132
+ it { expect(result.length).to eq 2 }
133
+ it { expect(result_names).to include "my-repo" }
134
+ it { expect(result_names).to include "new-repo" }
135
+ end
136
+
137
+ end # repositories
138
+
139
+ context "repositories with query" do
140
+
141
+ context "query = repo" do
142
+
143
+ let(:result) { registry_client.repositories.search("repo") }
144
+
145
+ context "check" do
146
+ let(:result_names) { result.map {|repo| repo["name"] } }
147
+ it { expect(result.length).to eq 2 }
148
+ it { expect(result_names).to include "my-repo" }
149
+ it { expect(result_names).to include "new-repo" }
150
+ end # check
151
+
152
+ end # query = repo
153
+
154
+ context "query = new" do
155
+
156
+ let(:result) { registry_client.repositories.search("new") }
157
+
158
+ context "check" do
159
+ let(:result_names) { result.map {|repo| repo["name"] } }
160
+ it { expect(result.length).to eq 1 }
161
+ it { expect(result_names).to_not include "my-repo" }
162
+ it { expect(result_names).to include "new-repo" }
163
+ end
164
+
165
+ end
166
+
167
+ end # repositories with query
168
+
169
+ end # create registry_client
170
+
171
+ end # RegistryClient
172
+
173
+ end # SocialSnippet