Sfoglia il codice sorgente

add first-draft implementation plus tests

Casey DeLorme 8 anni fa
parent
commit
0e80927a70
2 ha cambiato i file con 68 aggiunte e 0 eliminazioni
  1. 23 0
      main.go
  2. 45 0
      main_test.go

+ 23 - 0
main.go

@@ -0,0 +1,23 @@
+package main
+
+import (
+	"net/http"
+	"os"
+)
+
+func main() {
+	port := getPort()
+	wd, _ := os.Getwd()
+	http.ListenAndServe(":"+port, http.FileServer(http.Dir(wd)))
+}
+
+func getPort() string {
+	port := os.Getenv("PORT")
+	if len(os.Args) > 1 {
+		port = os.Args[1]
+	}
+	if len(port) == 0 {
+		port = "3000"
+	}
+	return port
+}

+ 45 - 0
main_test.go

@@ -0,0 +1,45 @@
+package main
+
+import (
+	"os"
+	"testing"
+)
+
+func TestPlacebo(t *testing.T) {
+	t.Parallel()
+	if !true {
+		t.FailNow()
+	}
+}
+
+func TestGetPortDefault(t *testing.T) {
+	os.Unsetenv("PORT")
+	os.Args = os.Args[:1]
+	if p := getPort(); p != "3000" {
+		t.Logf("expected 3000, got %s", p)
+		t.FailNow()
+	}
+}
+
+func TestGetPortEnv(t *testing.T) {
+	os.Setenv("PORT", "4000")
+	os.Args = os.Args[:1]
+	if p := getPort(); p != "4000" {
+		t.Logf("expected 4000, got %s", p)
+		t.FailNow()
+	}
+}
+
+func TestGetPortArgs(t *testing.T) {
+	os.Unsetenv("PORT")
+	os.Args = append(os.Args[:1], "5000")
+	if p := getPort(); p != "5000" {
+		t.Logf("expected 5000, got %s", p)
+		t.FailNow()
+	}
+}
+
+func TestMain(_ *testing.T) {
+	os.Args = append(os.Args[:1], "forced-failure")
+	main()
+}