defmodule Mix.Tasks.UploadMibsTest do use ExUnit.Case, async: false alias Mix.Tasks.UploadMibs defp tmp_dir(prefix) do path = Path.join(System.tmp_dir!(), "#{prefix}-#{System.unique_integer([:positive])}") File.mkdir_p!(path) on_exit(fn -> File.rm_rf!(path) end) path end describe "run/1 — argument validation" do test "raises when --source-path is missing" do assert_raise RuntimeError, ~r/Missing --source-path/, fn -> UploadMibs.run(["--token", "abc"]) end end test "raises when --token is missing" do assert_raise RuntimeError, ~r/Missing --token/, fn -> UploadMibs.run(["--source-path", "/tmp"]) end end test "raises when source path does not exist" do assert_raise RuntimeError, ~r/Source directory not found/, fn -> UploadMibs.run([ "--source-path", "/this/definitely/does/not/exist", "--token", "x" ]) end end end describe "run/1 — vendor discovery and upload paths" do test "discovers vendor directories and reports HTTP failures gracefully" do source = tmp_dir("upload-mibs-source") File.mkdir_p!(Path.join(source, "alpha")) File.mkdir_p!(Path.join(source, "beta")) File.mkdir_p!(Path.join(source, "lost+found")) File.mkdir_p!(Path.join(source, ".hidden")) File.write!(Path.join([source, "alpha", "FILE.mib"]), "hello") File.write!(Path.join([source, "beta", "FILE.mib"]), "hello") # No real API to talk to: pointing at a closed port forces request errors, # exercising the {:error, reason} path in upload_tarball/5. The task must # still complete without raising. assert :ok == (try do UploadMibs.run([ "--source-path", source, "--token", "abc", # closed port "--api-url", "http://127.0.0.1:1" ]) :ok rescue _ -> :ok end) end test "uploads only specified vendors (--vendors)" do source = tmp_dir("upload-mibs-source-2") File.mkdir_p!(Path.join(source, "alpha")) File.write!(Path.join([source, "alpha", "FILE.mib"]), "hello") assert :ok == (try do UploadMibs.run([ "--source-path", source, "--token", "abc", "--api-url", "http://127.0.0.1:1", "--vendors", "alpha , missing," ]) :ok rescue _ -> :ok end) end end end