File: repository_spec.rb

package info (click to toggle)
ruby-octokit 10.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,092 kB
  • sloc: ruby: 13,339; sh: 99; makefile: 7; javascript: 3
file content (178 lines) | stat: -rw-r--r-- 7,768 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# frozen_string_literal: true

describe Octokit::Repository do
  context 'when passed a string containg a slash' do
    before do
      @repository = Octokit::Repository.new('sferik/octokit')
    end

    it 'sets the repository name and username' do
      expect(@repository.name).to eq('octokit')
      expect(@repository.username).to eq('sferik')
    end

    it 'responds to repo and user' do
      expect(@repository.repo).to eq('octokit')
      expect(@repository.user).to eq('sferik')
    end

    it 'renders slug as string' do
      expect(@repository.slug).to eq('sferik/octokit')
      expect(@repository.to_s).to eq(@repository.slug)
    end

    it 'renders url as string' do
      expect(@repository.url).to eq('https://github.com/sferik/octokit')
    end
  end

  context 'when passed a string without a slash' do
    it 'raises ArgumentError' do
      expect { Octokit::Repository.new('raise-error') }
        .to raise_error Octokit::InvalidRepository, '"raise-error" is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys.'
    end
  end

  context 'when passed a string with more than 1 slash' do
    it 'raises ArgumentError' do
      expect { Octokit::Repository.new('more_than/one/slash') }
        .to raise_error Octokit::InvalidRepository, '"more_than/one/slash" is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys.'
    end
  end

  context 'when passed an invalid path' do
    it 'raises ArgumentError' do
      expect { Octokit::Repository.new('invalid / path') }
        .to raise_error Octokit::InvalidRepository, '"invalid / path" is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys.'
    end
  end

  context 'when passed a boolean true' do
    it 'raises ArgumentError' do
      expect { Octokit::Repository.new(true) }
        .to raise_error Octokit::InvalidRepository, 'true is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys.'
    end
  end

  context 'when passed a boolean false' do
    it 'false raises ArgumentError' do
      expect { Octokit::Repository.new(false) }
        .to raise_error Octokit::InvalidRepository, 'false is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys.'
    end
  end

  context 'when passed nil' do
    it 'raises ArgumentError' do
      expect { Octokit::Repository.new(nil) }
        .to raise_error Octokit::InvalidRepository, 'nil is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys.'
    end
  end

  describe '.path' do
    context 'with named repository' do
      it 'returns the url path' do
        repository = Octokit::Repository.new('sferik/octokit')
        expect(repository.path).to eq 'repos/sferik/octokit'
      end
    end

    context 'with repository id' do
      it 'returns theu url path' do
        repository = Octokit::Repository.new(12_345)
        expect(repository.path).to eq 'repositories/12345'
      end
    end
  end # .path

  describe 'self.path' do
    it 'returns the api path' do
      expect(Octokit::Repository.path('sferik/octokit')).to eq 'repos/sferik/octokit'
      expect(Octokit::Repository.path(12_345)).to eq 'repositories/12345'
    end
  end

  context 'when passed an integer' do
    it 'sets the repository id' do
      repository = Octokit::Repository.new(12_345)
      expect(repository.id).to eq 12_345
    end
  end

  context 'when passed a hash' do
    it 'sets the repository name and username' do
      repository = Octokit::Repository.new({ username: 'sferik', name: 'octokit' })
      expect(repository.name).to eq('octokit')
      expect(repository.username).to eq('sferik')
    end
  end

  context 'when passed a frozen hash' do
    it 'sets the repository name and username' do
      repository = Octokit::Repository.new({ username: 'sferik', name: 'octokit' }.freeze)
      expect(repository.name).to eq('octokit')
      expect(repository.username).to eq('sferik')
    end
  end

  context 'when passed a hash with invalid username' do
    it 'raises ArgumentError' do
      expect { Octokit::Repository.new({ username: 'invalid username!', name: 'octokit' }) }
        .to raise_error Octokit::InvalidRepository, "#{{ username: 'invalid username!', name: 'octokit' }.inspect} is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys."
    end
  end

  context 'when passed a hash with a username that contains a slash' do
    it 'raises ArgumentError' do
      expect { Octokit::Repository.new({ username: 'invalid/username', name: 'octokit' }) }
        .to raise_error Octokit::InvalidRepository, "#{{ username: 'invalid/username', name: 'octokit' }.inspect} is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys."
    end
  end

  context 'when passed a hash with invalid repo' do
    it 'raises ArgumentError' do
      expect { Octokit::Repository.new({ username: 'sferik', name: 'invalid repo!' }) }
        .to raise_error Octokit::InvalidRepository, "#{{ username: 'sferik', name: 'invalid repo!' }.inspect} is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys."
    end
  end

  context 'when passed a hash with a repo that contains a slash' do
    it 'raises ArgumentError' do
      expect { Octokit::Repository.new({ username: 'sferik', name: 'invalid/repo' }) }
        .to raise_error Octokit::InvalidRepository, "#{{ username: 'sferik', name: 'invalid/repo' }.inspect} is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys."
    end
  end

  context 'when passed a Repo' do
    it 'sets the repository name and username' do
      repository = Octokit::Repository.new(Octokit::Repository.new('sferik/octokit'))
      expect(repository.name).to eq('octokit')
      expect(repository.username).to eq('sferik')
      expect(repository.url).to eq('https://github.com/sferik/octokit')
    end
  end

  context 'when given a URL' do
    it 'sets the repository name and username' do
      repository = Octokit::Repository.from_url('https://github.com/sferik/octokit')
      expect(repository.name).to eq('octokit')
      expect(repository.username).to eq('sferik')
    end

    it 'parses api urls' do
      repo_from_url = Octokit::Repository.from_url('https://api.github.com/repos/sferik/octokit-repos/issues/21')
      expect(repo_from_url.name).to eq('octokit-repos')
      expect(repo_from_url.username).to eq('sferik')
    end

    it 'parses html urls' do
      repo_from_url = Octokit::Repository.from_url('https://github.com/sferik/octokit/issues/132')
      expect(repo_from_url.name).to eq('octokit')
      expect(repo_from_url.username).to eq('sferik')
    end

    it 'raises InvalidRepository error for unsupported url' do
      expect { Octokit::Repository.new('https://api.github.com/gists/0083ae') }
        .to raise_error Octokit::InvalidRepository, '"https://api.github.com/gists/0083ae" is invalid as a repository identifier. Use the user/repo (String) format, or the repository ID (Integer), or a hash containing :repo and :user keys.'
    end
  end
end