Last active
December 15, 2015 12:59
-
-
Save Andrew8xx8/5264400 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Gitlab | |
| class SnippetRepo | |
| attr_reader :path, :repo | |
| def initialize(path) | |
| @path = path | |
| @repo = Grit::Repo.new path | |
| rescue Grit::InvalidGitRepositoryError, Grit::NoSuchPathError | |
| @repo = Grit::Repo.init path | |
| end | |
| def add_file(filename, content) | |
| Dir.chdir(@path) do | |
| File.open(filename, 'w') { |f| f.write(content) } | |
| @repo.add(filename) | |
| @repo.commit_index("Added #{filename}") | |
| end | |
| end | |
| def update_file(filename, content) | |
| Dir.chdir(@path) do | |
| File.open(filename, 'w') { |f| f.write(content) } | |
| @repo.add(filename) | |
| @repo.commit_index("Updated #{filename}") | |
| end | |
| end | |
| def delete_file(filename) | |
| Dir.chdir(@path) do | |
| @repo.remove(filename) | |
| @repo.commit_index("Deleted #{filename}") | |
| end | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'spec_helper' | |
| describe 'Gitlab::SnippetRepo' do | |
| let (:filename) { 'snippet_' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0') } | |
| let (:snippet_repo) { Gitlab::SnippetRepo.new(snippet_path) } | |
| let (:snippet_path) do | |
| path = Rails.root.join('tmp', 'test_snippets', filename).to_s | |
| FileUtils.mkdir_p path unless Dir.exists? path | |
| path | |
| end | |
| it "should create new snippet repository by path" do | |
| new_snippet_repo = Gitlab::SnippetRepo.new(snippet_path) | |
| new_snippet_repo.repo.should be_a_kind_of(Grit::Repo) | |
| end | |
| it "should initialize snippet repository by path" do | |
| snippet_repo.repo.should be_a_kind_of(Grit::Repo) | |
| end | |
| it "should add the file to the snippet repository" do | |
| snippet_repo.add_file filename, 'test content' | |
| snippet_repo.repo.log.first.message.should eq "Added #{filename}" | |
| end | |
| it "should delete the file from the snipept repository" do | |
| FileUtils.touch File.join(snippet_repo.path, filename) | |
| snippet_repo.add_file filename, 'test content' | |
| snippet_repo.update_file filename, 'new test content' | |
| snippet_repo.repo.log.first.message.should eq "Updated #{filename}" | |
| end | |
| it "should update the file in the snippet repository" do | |
| FileUtils.touch File.join(snippet_repo.path, filename) | |
| snippet_repo.add_file filename, 'test content' | |
| snippet_repo.delete_file filename | |
| snippet_repo.repo.log.first.message.should eq "Deleted #{filename}" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment