| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | require "rails_helper" |
| 4 | require "tmpdir" |
| 5 | require "fileutils" |
| 6 | |
| 7 | # Checks that every code-browsing page (repo root, tree, blob, commits) offers |
| 8 | # a way to switch branches, lists all branches, and that following a branch |
| 9 | # link actually lands on that branch's content. |
| 10 | RSpec.describe "Branch switching", type: :request do |
| 11 | around do |example| |
| 12 | Dir.mktmpdir("branch-switcher-spec") do |tmp| |
| 13 | @repo_dir = File.join(tmp, "demo.git") |
| 14 | build_repo(@repo_dir) |
| 15 | example.run |
| 16 | end |
| 17 | end |
| 18 | |
| 19 | let(:user) { User.create!(smbcloud_id: 555_321, email: "branch-spec@example.com", username: "branchspec") } |
| 20 | let!(:repo) { user.repositories.create!(name: "demo", disk_path: @repo_dir, default_branch: "main") } |
| 21 | |
| 22 | it "lists all branches and marks the current one on the repo root" do |
| 23 | get "/branchspec/demo" |
| 24 | expect(response.body).to include(">main<") |
| 25 | expect(response.body).to include('href="/branchspec/demo?branch=develop"') |
| 26 | end |
| 27 | |
| 28 | it "switches content when following a branch link on the repo root" do |
| 29 | get "/branchspec/demo?branch=develop" |
| 30 | expect(response.body).to include("only-on-develop.txt") |
| 31 | end |
| 32 | |
| 33 | it "offers branch switching on the tree page and preserves the current path" do |
| 34 | get "/branchspec/demo/tree/main/dir" |
| 35 | expect(response.body).to include('href="/branchspec/demo/tree/develop/dir"') |
| 36 | end |
| 37 | |
| 38 | it "offers branch switching on the blob page for the current file" do |
| 39 | get "/branchspec/demo/blob/main/README.md" |
| 40 | expect(response.body).to include('href="/branchspec/demo/blob/develop/README.md"') |
| 41 | end |
| 42 | |
| 43 | it "offers branch switching on the commits page" do |
| 44 | get "/branchspec/demo/commits/main" |
| 45 | expect(response.body).to include('href="/branchspec/demo/commits/develop"') |
| 46 | end |
| 47 | |
| 48 | def build_repo(bare_path) |
| 49 | FileUtils.mkdir_p(bare_path) |
| 50 | system("git", "init", "--bare", bare_path, exception: true) |
| 51 | Dir.mktmpdir do |work| |
| 52 | system("git", "-C", work, "init", "-b", "main", exception: true) |
| 53 | system("git", "-C", work, "config", "user.email", "t@example.com", exception: true) |
| 54 | system("git", "-C", work, "config", "user.name", "Test", exception: true) |
| 55 | File.write(File.join(work, "README.md"), "# Demo\n") |
| 56 | FileUtils.mkdir_p(File.join(work, "dir")) |
| 57 | File.write(File.join(work, "dir", "file.txt"), "hi\n") |
| 58 | system("git", "-C", work, "add", ".", exception: true) |
| 59 | system("git", "-C", work, "commit", "-m", "seed", exception: true) |
| 60 | system("git", "-C", work, "remote", "add", "origin", bare_path, exception: true) |
| 61 | system("git", "-C", work, "push", "origin", "main", exception: true) |
| 62 | |
| 63 | system("git", "-C", work, "checkout", "-b", "develop", exception: true) |
| 64 | File.write(File.join(work, "only-on-develop.txt"), "only on develop\n") |
| 65 | system("git", "-C", work, "add", ".", exception: true) |
| 66 | system("git", "-C", work, "commit", "-m", "develop-only file", exception: true) |
| 67 | system("git", "-C", work, "push", "origin", "develop", exception: true) |
| 68 | end |
| 69 | end |
| 70 | end |