seed_helper.rb 3.39 KB
Newer Older
1 2
require_relative 'test_env'

Robert Speicher's avatar
Robert Speicher committed
3 4
# This file is specific to specs in spec/lib/gitlab/git/

5 6 7 8 9
SEED_STORAGE_PATH      = TestEnv.repos_path
TEST_REPO_PATH         = 'gitlab-git-test.git'.freeze
TEST_NORMAL_REPO_PATH  = 'not-bare-repo.git'.freeze
TEST_MUTABLE_REPO_PATH = 'mutable-repo.git'.freeze
TEST_BROKEN_REPO_PATH  = 'broken-repo.git'.freeze
Robert Speicher's avatar
Robert Speicher committed
10 11

module SeedHelper
12
  GITLAB_GIT_TEST_REPO_URL = ENV.fetch('GITLAB_GIT_TEST_REPO_URL', 'https://gitlab.com/gitlab-org/gitlab-git-test.git').freeze
Robert Speicher's avatar
Robert Speicher committed
13 14

  def ensure_seeds
15 16
    if File.exist?(SEED_STORAGE_PATH)
      FileUtils.rm_r(SEED_STORAGE_PATH)
Robert Speicher's avatar
Robert Speicher committed
17 18
    end

19
    FileUtils.mkdir_p(SEED_STORAGE_PATH)
Robert Speicher's avatar
Robert Speicher committed
20 21 22 23 24 25 26 27 28 29

    create_bare_seeds
    create_normal_seeds
    create_mutable_seeds
    create_broken_seeds
    create_git_attributes
    create_invalid_git_attributes
  end

  def create_bare_seeds
30
    system(git_env, *%W(#{Gitlab.config.git.bin_path} clone --bare #{GITLAB_GIT_TEST_REPO_URL}),
31
           chdir: SEED_STORAGE_PATH,
Robert Speicher's avatar
Robert Speicher committed
32 33 34 35 36
           out:   '/dev/null',
           err:   '/dev/null')
  end

  def create_normal_seeds
37
    system(git_env, *%W(#{Gitlab.config.git.bin_path} clone #{TEST_REPO_PATH} #{TEST_NORMAL_REPO_PATH}),
38
           chdir: SEED_STORAGE_PATH,
Robert Speicher's avatar
Robert Speicher committed
39 40 41 42 43
           out: '/dev/null',
           err: '/dev/null')
  end

  def create_mutable_seeds
44
    system(git_env, *%W(#{Gitlab.config.git.bin_path} clone #{TEST_REPO_PATH} #{TEST_MUTABLE_REPO_PATH}),
45
           chdir: SEED_STORAGE_PATH,
Robert Speicher's avatar
Robert Speicher committed
46 47 48
           out: '/dev/null',
           err: '/dev/null')

49 50 51
    mutable_repo_full_path = File.join(SEED_STORAGE_PATH, TEST_MUTABLE_REPO_PATH)
    system(git_env, *%W(#{Gitlab.config.git.bin_path} branch -t feature origin/feature),
           chdir: mutable_repo_full_path, out: '/dev/null', err: '/dev/null')
Robert Speicher's avatar
Robert Speicher committed
52

53
    system(git_env, *%W(#{Gitlab.config.git.bin_path} remote add expendable #{GITLAB_GIT_TEST_REPO_URL}),
54
           chdir: mutable_repo_full_path, out: '/dev/null', err: '/dev/null')
Robert Speicher's avatar
Robert Speicher committed
55 56 57
  end

  def create_broken_seeds
58
    system(git_env, *%W(#{Gitlab.config.git.bin_path} clone --bare #{TEST_REPO_PATH} #{TEST_BROKEN_REPO_PATH}),
59
           chdir: SEED_STORAGE_PATH,
Robert Speicher's avatar
Robert Speicher committed
60 61 62
           out: '/dev/null',
           err: '/dev/null')

63
    refs_path = File.join(SEED_STORAGE_PATH, TEST_BROKEN_REPO_PATH, 'refs')
Robert Speicher's avatar
Robert Speicher committed
64 65 66 67 68

    FileUtils.rm_r(refs_path)
  end

  def create_git_attributes
69
    dir = File.join(SEED_STORAGE_PATH, 'with-git-attributes.git', 'info')
Robert Speicher's avatar
Robert Speicher committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    FileUtils.mkdir_p(dir)

    File.open(File.join(dir, 'attributes'), 'w') do |handle|
      handle.write <<-EOF.strip
# This is a comment, it should be ignored.

*.txt     text
*.jpg     -text
*.sh      eol=lf gitlab-language=shell
*.haml.*  gitlab-language=haml
foo/bar.* foo
*.cgi     key=value?p1=v1&p2=v2
/*.png    gitlab-language=png
*.binary  binary

# This uses a tab instead of spaces to ensure the parser also supports this.
*.md\tgitlab-language=markdown
bla/bla.txt
      EOF
    end
  end

  def create_invalid_git_attributes
94
    dir = File.join(SEED_STORAGE_PATH, 'with-invalid-git-attributes.git', 'info')
Robert Speicher's avatar
Robert Speicher committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

    FileUtils.mkdir_p(dir)

    enc = Encoding::UTF_16

    File.open(File.join(dir, 'attributes'), 'w', encoding: enc) do |handle|
      handle.write('# hello'.encode(enc))
    end
  end

  # Prevent developer git configurations from being persisted to test
  # repositories
  def git_env
    { 'GIT_TEMPLATE_DIR' => '' }
  end
end

RSpec.configure do |config|
  config.include SeedHelper, :seed_helper

  config.before(:all, :seed_helper) do
    ensure_seeds
  end
end