|
| 1 | +defmodule Mix.Tasks.Igniter.NewTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + @moduletag :tmp_dir |
| 5 | + |
| 6 | + describe "igniter.new with --git flag" do |
| 7 | + @tag :integration |
| 8 | + test "initializes git repository and creates initial commit", %{tmp_dir: tmp_dir} do |
| 9 | + unless git_available?() do |
| 10 | + :ok |
| 11 | + else |
| 12 | + IO.inspect(tmp_dir) |
| 13 | + project_name = "test_project" |
| 14 | + project_path = Path.join(tmp_dir, project_name) |
| 15 | + |
| 16 | + # Change to tmp directory to run the command |
| 17 | + original_cwd = File.cwd!() |
| 18 | + |
| 19 | + try do |
| 20 | + File.cd!(tmp_dir) |
| 21 | + |
| 22 | + # Run igniter.new with --git flag using System.cmd |
| 23 | + {output, exit_code} = |
| 24 | + System.cmd( |
| 25 | + "mix", |
| 26 | + ["igniter.new", project_name, "--git", "--yes", "--no-installer-version-check"], |
| 27 | + stderr_to_stdout: true, |
| 28 | + env: [{"MIX_ENV", "test"}] |
| 29 | + ) |
| 30 | + |
| 31 | + if exit_code != 0 do |
| 32 | + IO.puts("Command failed with output: #{output}") |
| 33 | + end |
| 34 | + |
| 35 | + assert exit_code == 0, |
| 36 | + "Expected igniter.new to succeed, but got exit code #{exit_code}. Output: #{output}" |
| 37 | + |
| 38 | + # Verify project directory was created |
| 39 | + assert File.exists?(project_path), "Project directory should exist at #{project_path}" |
| 40 | + |
| 41 | + # Change to project directory to check git status |
| 42 | + File.cd!(project_path) |
| 43 | + |
| 44 | + # Verify .git directory exists |
| 45 | + assert File.exists?(".git"), ".git directory should exist" |
| 46 | + assert File.dir?(".git"), ".git should be a directory" |
| 47 | + |
| 48 | + # Verify git repository was initialized by checking git status |
| 49 | + {output, 0} = System.cmd("git", ["status", "--porcelain"]) |
| 50 | + # Empty output means all files are committed (working directory clean) |
| 51 | + assert String.trim(output) == "", |
| 52 | + "Working directory should be clean after initial commit" |
| 53 | + |
| 54 | + # Verify there is at least one commit |
| 55 | + {output, 0} = System.cmd("git", ["log", "--oneline"]) |
| 56 | + |
| 57 | + assert String.contains?(output, "🔥 initial commit 🔥"), |
| 58 | + "Should have initial commit with fire emoji" |
| 59 | + |
| 60 | + # Verify essential files are tracked |
| 61 | + {output, 0} = System.cmd("git", ["ls-files"]) |
| 62 | + tracked_files = String.split(String.trim(output), "\n") |
| 63 | + |
| 64 | + assert "mix.exs" in tracked_files, "mix.exs should be tracked" |
| 65 | + assert ".gitignore" in tracked_files, ".gitignore should be tracked" |
| 66 | + assert "README.md" in tracked_files, "README.md should be tracked" |
| 67 | + |
| 68 | + # Verify igniter was added to mix.exs |
| 69 | + mix_exs_content = File.read!("mix.exs") |
| 70 | + |
| 71 | + assert String.contains?(mix_exs_content, "{:igniter"), |
| 72 | + "Igniter dependency should be added to mix.exs" |
| 73 | + after |
| 74 | + File.cd!(original_cwd) |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + @tag :integration |
| 80 | + test "creates project without git when --git flag is not provided", %{tmp_dir: tmp_dir} do |
| 81 | + unless git_available?() do |
| 82 | + :ok |
| 83 | + else |
| 84 | + project_name = "test_project_no_git" |
| 85 | + project_path = Path.join(tmp_dir, project_name) |
| 86 | + |
| 87 | + original_cwd = File.cwd!() |
| 88 | + |
| 89 | + try do |
| 90 | + File.cd!(tmp_dir) |
| 91 | + |
| 92 | + # Run igniter.new without --git flag |
| 93 | + {output, exit_code} = |
| 94 | + System.cmd( |
| 95 | + "mix", |
| 96 | + ["igniter.new", project_name, "--yes", "--no-installer-version-check"], |
| 97 | + stderr_to_stdout: true, |
| 98 | + env: [{"MIX_ENV", "test"}] |
| 99 | + ) |
| 100 | + |
| 101 | + assert exit_code == 0, |
| 102 | + "Expected igniter.new to succeed, but got exit code #{exit_code}. Output: #{output}" |
| 103 | + |
| 104 | + assert File.exists?(project_path), "Project directory should exist" |
| 105 | + File.cd!(project_path) |
| 106 | + |
| 107 | + # Verify .git directory does NOT exist |
| 108 | + refute File.exists?(".git"), |
| 109 | + ".git directory should not exist when --git flag is not provided" |
| 110 | + after |
| 111 | + File.cd!(original_cwd) |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + @tag :integration |
| 117 | + test "git functionality works with other flags", %{tmp_dir: tmp_dir} do |
| 118 | + unless git_available?() do |
| 119 | + :ok |
| 120 | + else |
| 121 | + project_name = "test_project_with_sup" |
| 122 | + project_path = Path.join(tmp_dir, project_name) |
| 123 | + |
| 124 | + original_cwd = File.cwd!() |
| 125 | + |
| 126 | + try do |
| 127 | + File.cd!(tmp_dir) |
| 128 | + |
| 129 | + # Run igniter.new with --git and --sup flags |
| 130 | + {output, exit_code} = |
| 131 | + System.cmd( |
| 132 | + "mix", |
| 133 | + [ |
| 134 | + "igniter.new", |
| 135 | + project_name, |
| 136 | + "--git", |
| 137 | + "--sup", |
| 138 | + "--yes", |
| 139 | + "--no-installer-version-check" |
| 140 | + ], |
| 141 | + stderr_to_stdout: true, |
| 142 | + env: [{"MIX_ENV", "test"}] |
| 143 | + ) |
| 144 | + |
| 145 | + assert exit_code == 0, |
| 146 | + "Expected igniter.new to succeed, but got exit code #{exit_code}. Output: #{output}" |
| 147 | + |
| 148 | + assert File.exists?(project_path) |
| 149 | + File.cd!(project_path) |
| 150 | + |
| 151 | + # Verify git repository exists and is clean |
| 152 | + assert File.exists?(".git") |
| 153 | + {output, 0} = System.cmd("git", ["status", "--porcelain"]) |
| 154 | + assert String.trim(output) == "" |
| 155 | + |
| 156 | + # Verify project has supervision tree (--sup flag worked) |
| 157 | + mix_exs_content = File.read!("mix.exs") |
| 158 | + |
| 159 | + assert String.contains?(mix_exs_content, "mod: {"), |
| 160 | + "Should have supervision tree when --sup is used" |
| 161 | + |
| 162 | + # Verify commit exists |
| 163 | + {output, 0} = System.cmd("git", ["rev-list", "--count", "HEAD"]) |
| 164 | + commit_count = String.trim(output) |> String.to_integer() |
| 165 | + assert commit_count >= 1, "Should have at least one commit" |
| 166 | + after |
| 167 | + File.cd!(original_cwd) |
| 168 | + end |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | + |
| 173 | + describe "handle missing git" do |
| 174 | + @tag :integration |
| 175 | + test "handles missing git gracefully in development", %{tmp_dir: tmp_dir} do |
| 176 | + # This test simulates what happens when git is not available |
| 177 | + # but --git flag is used. In practice, the initialize_git_repo function |
| 178 | + # will output error messages but not crash the entire task. |
| 179 | + |
| 180 | + project_name = "test_project_no_git_binary" |
| 181 | + project_path = Path.join(tmp_dir, project_name) |
| 182 | + |
| 183 | + original_cwd = File.cwd!() |
| 184 | + |
| 185 | + try do |
| 186 | + File.cd!(tmp_dir) |
| 187 | + |
| 188 | + # Create project first without git |
| 189 | + {_output, exit_code} = |
| 190 | + System.cmd( |
| 191 | + "mix", |
| 192 | + ["igniter.new", project_name, "--yes", "--no-installer-version-check"], |
| 193 | + stderr_to_stdout: true, |
| 194 | + env: [{"MIX_ENV", "test"}] |
| 195 | + ) |
| 196 | + |
| 197 | + assert exit_code == 0 |
| 198 | + assert File.exists?(project_path) |
| 199 | + |
| 200 | + File.cd!(project_path) |
| 201 | + |
| 202 | + # Now test that the git repo functionality produces appropriate output |
| 203 | + # when git commands fail (we can't easily simulate missing git binary in tests) |
| 204 | + if git_available?() do |
| 205 | + # At least verify that git operations would work in normal circumstances |
| 206 | + {_output, exit_code} = System.cmd("git", ["init"]) |
| 207 | + assert exit_code == 0 |
| 208 | + end |
| 209 | + after |
| 210 | + File.cd!(original_cwd) |
| 211 | + end |
| 212 | + end |
| 213 | + end |
| 214 | + |
| 215 | + # Helper to ensure git is available for testing |
| 216 | + defp git_available? do |
| 217 | + case System.cmd("git", ["--version"]) do |
| 218 | + {_, 0} -> true |
| 219 | + _ -> false |
| 220 | + end |
| 221 | + end |
| 222 | +end |
0 commit comments