From 08c0838c83c31f80278466ec36084e786687b1d9 Mon Sep 17 00:00:00 2001
From: leaf corcoran <leafot@gmail.com>
Date: Mon, 9 Jan 2023 13:14:14 -0800
Subject: [PATCH] add some environment override tests

---
 lapis/cmd/actions.lua  | 16 ++++++++++++++++
 lapis/cmd/actions.moon | 14 ++++++++++++++
 spec/cmd_spec.moon     | 22 ++++++++++++++++++++++
 3 files changed, 52 insertions(+)

diff --git a/lapis/cmd/actions.lua b/lapis/cmd/actions.lua
index da08349f..c6cefcdf 100644
--- a/lapis/cmd/actions.lua
+++ b/lapis/cmd/actions.lua
@@ -350,6 +350,22 @@ local COMMANDS = {
       args["preload-module"] = args.preload_module
       return action[1](self, args, unpack(args.files))
     end
+  },
+  {
+    name = "debug",
+    hidden = true,
+    help = "Debug information for test sutie",
+    test_available = function()
+      local running_in_test
+      running_in_test = require("lapis.spec").running_in_test
+      return running_in_test()
+    end,
+    argparse = function(command)
+      return add_environment_argument(command)
+    end,
+    function(self, args)
+      return args
+    end
   }
 }
 local CommandRunner
diff --git a/lapis/cmd/actions.moon b/lapis/cmd/actions.moon
index 8be12bf0..feab036d 100644
--- a/lapis/cmd/actions.moon
+++ b/lapis/cmd/actions.moon
@@ -318,6 +318,20 @@ COMMANDS = {
       args["preload-module"] = args.preload_module
       action[1] @, args, unpack args.files
   }
+
+  {
+    name: "debug"
+    hidden: true
+    help: "Debug information for test sutie"
+    test_available: ->
+      import running_in_test from require "lapis.spec"
+      running_in_test!
+
+    argparse: (command) ->
+      add_environment_argument command
+
+    (args) => args
+  }
 }
 
 class CommandRunner
diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon
index cc62ae01..bf3d30e3 100644
--- a/spec/cmd_spec.moon
+++ b/spec/cmd_spec.moon
@@ -173,6 +173,28 @@ describe "lapis.cmd.actions.execute", ->
     table.sort have_files
     assert.same files, have_files
 
+  describe "debug", ->
+    it "gets default environment with no overrides", ->
+      res = cmd.execute { "debug" }
+      assert.same "test", res.environment
+
+    it "environment with --environment", ->
+      res = cmd.execute { "debug", "--environment", "cool" }
+      assert.same "cool", res.environment
+
+      res = cmd.execute { "--environment", "cool", "debug" }
+      assert.same "cool", res.environment
+
+    it "environment with arg", ->
+      res = cmd.execute { "debug", "wow" }
+      assert.same "wow", res.environment
+
+    it "fails with double env", ->
+      assert.has_error(
+        -> cmd.execute { "--environment=umm", "debug", "wow" }
+        "You tried to set the environment twice. Use either --environment or the environment argument, not both"
+      )
+
   describe "new", ->
     before_each ->
       stub(require("lapis.cmd.nginx"), "find_nginx").returns true