MCPcopy
hub / github.com/air-verse/air / TestEngineLoadEnvFile

Function TestEngineLoadEnvFile

runner/engine_test.go:1782–1833  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1780}
1781
1782func TestEngineLoadEnvFile(t *testing.T) {
1783 tmpDir := t.TempDir()
1784 envPath := filepath.Join(tmpDir, ".env")
1785
1786 originalValue := "original_global_value"
1787 t.Setenv("TEST_GLOBAL_VAR", originalValue)
1788
1789 const initialEnv = `TEST_VAR1=value1
1790TEST_VAR2=value2
1791TEST_GLOBAL_VAR=overridden_value
1792`
1793
1794 err := os.WriteFile(envPath, []byte(initialEnv), 0o644)
1795 require.NoError(t, err)
1796
1797 cfg := defaultConfig()
1798 cfg.Root = tmpDir
1799 cfg.EnvFiles = []string{".env"}
1800
1801 engine, err := NewEngineWithConfig(&cfg, false)
1802 require.NoError(t, err)
1803
1804 engine.loadEnvFile()
1805
1806 assert.Equal(t, "value1", os.Getenv("TEST_VAR1"), "TEST_VAR1 should be set")
1807 assert.Equal(t, "value2", os.Getenv("TEST_VAR2"), "TEST_VAR2 should be set")
1808 assert.Equal(t, "original_global_value", os.Getenv("TEST_GLOBAL_VAR"), "TEST_GLOBAL_VAR should NOT be overridden")
1809
1810 // remove TEST_VAR2
1811 const updatedEnv = `TEST_VAR1=updated_value1
1812TEST_GLOBAL_VAR=still_overridden
1813`
1814 err = os.WriteFile(envPath, []byte(updatedEnv), 0o644)
1815 require.NoError(t, err)
1816
1817 engine.loadEnvFile()
1818
1819 assert.Equal(t, "updated_value1", os.Getenv("TEST_VAR1"), "TEST_VAR1 should be updated")
1820 // since TEST_VAR2 only exists in environment thanks to air, it should get unset on removal
1821 _, exists := os.LookupEnv("TEST_VAR2")
1822 assert.False(t, exists, "TEST_VAR2 should be unset after removal from .env")
1823 assert.Equal(t, "original_global_value", os.Getenv("TEST_GLOBAL_VAR"), "TEST_GLOBAL_VAR should NOT be overridden")
1824
1825 const finalEnv = `TEST_VAR1=final_value`
1826 err = os.WriteFile(envPath, []byte(finalEnv), 0o644)
1827 require.NoError(t, err)
1828
1829 engine.loadEnvFile()
1830
1831 assert.Equal(t, "final_value", os.Getenv("TEST_VAR1"), "TEST_VAR1 should be final")
1832 assert.Equal(t, originalValue, os.Getenv("TEST_GLOBAL_VAR"), "TEST_GLOBAL_VAR should be restored to original value")
1833}

Callers

nothing calls this directly

Calls 3

defaultConfigFunction · 0.85
NewEngineWithConfigFunction · 0.85
loadEnvFileMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…