|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestGetRootPath(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + servicePrefix string |
| 12 | + expected string |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "standard service prefix", |
| 16 | + servicePrefix: "/user/fakeuser/myapp/", |
| 17 | + expected: "/hub/user/fakeuser/myapp", |
| 18 | + }, |
| 19 | + { |
| 20 | + name: "service prefix without trailing slash", |
| 21 | + servicePrefix: "/user/testuser/app", |
| 22 | + expected: "/hub/user/testuser/app", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "service prefix without leading slash", |
| 26 | + servicePrefix: "user/demouser/app/", |
| 27 | + expected: "/hub/user/demouser/app", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "empty service prefix", |
| 31 | + servicePrefix: "", |
| 32 | + expected: "", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "simple service prefix", |
| 36 | + servicePrefix: "/user/alice/", |
| 37 | + expected: "/hub/user/alice", |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + for _, tt := range tests { |
| 42 | + t.Run(tt.name, func(t *testing.T) { |
| 43 | + // Set environment variable |
| 44 | + if tt.servicePrefix != "" { |
| 45 | + os.Setenv("JUPYTERHUB_SERVICE_PREFIX", tt.servicePrefix) |
| 46 | + } else { |
| 47 | + os.Unsetenv("JUPYTERHUB_SERVICE_PREFIX") |
| 48 | + } |
| 49 | + defer os.Unsetenv("JUPYTERHUB_SERVICE_PREFIX") |
| 50 | + |
| 51 | + result := GetRootPath() |
| 52 | + if result != tt.expected { |
| 53 | + t.Errorf("GetRootPath() = %q, want %q", result, tt.expected) |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestSubstitutePort(t *testing.T) { |
| 60 | + tests := []struct { |
| 61 | + name string |
| 62 | + command []string |
| 63 | + port int |
| 64 | + servicePrefix string |
| 65 | + expected []string |
| 66 | + }{ |
| 67 | + { |
| 68 | + name: "substitute port only", |
| 69 | + command: []string{"python", "-m", "http.server", "{port}"}, |
| 70 | + port: 8080, |
| 71 | + servicePrefix: "", |
| 72 | + expected: []string{"python", "-m", "http.server", "8080"}, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "substitute root_path only", |
| 76 | + command: []string{"myapp", "--root-path", "{root_path}"}, |
| 77 | + port: 8080, |
| 78 | + servicePrefix: "/user/test/app/", |
| 79 | + expected: []string{"myapp", "--root-path", "/hub/user/test/app"}, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "substitute both port and root_path", |
| 83 | + command: []string{"myapp", "--port", "{port}", "--root-path", "{root_path}"}, |
| 84 | + port: 9000, |
| 85 | + servicePrefix: "/user/bob/dashboard/", |
| 86 | + expected: []string{"myapp", "--port", "9000", "--root-path", "/hub/user/bob/dashboard"}, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "substitute dash placeholders", |
| 90 | + command: []string{"myapp", "{-}p", "{port}", "{--}root-path", "{root_path}"}, |
| 91 | + port: 8888, |
| 92 | + servicePrefix: "/user/test/", |
| 93 | + expected: []string{"myapp", "-p", "8888", "--root-path", "/hub/user/test"}, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "strip single quotes", |
| 97 | + command: []string{"'myapp --port {port}'"}, |
| 98 | + port: 3000, |
| 99 | + servicePrefix: "", |
| 100 | + expected: []string{"myapp --port 3000"}, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "strip double quotes", |
| 104 | + command: []string{`"myapp --root-path {root_path}"`}, |
| 105 | + port: 3000, |
| 106 | + servicePrefix: "/user/demo/", |
| 107 | + expected: []string{"myapp --root-path /hub/user/demo"}, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "empty root_path when no service prefix", |
| 111 | + command: []string{"myapp", "--root-path", "{root_path}"}, |
| 112 | + port: 5000, |
| 113 | + servicePrefix: "", |
| 114 | + expected: []string{"myapp", "--root-path", ""}, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for _, tt := range tests { |
| 119 | + t.Run(tt.name, func(t *testing.T) { |
| 120 | + // Set environment variable |
| 121 | + if tt.servicePrefix != "" { |
| 122 | + os.Setenv("JUPYTERHUB_SERVICE_PREFIX", tt.servicePrefix) |
| 123 | + } else { |
| 124 | + os.Unsetenv("JUPYTERHUB_SERVICE_PREFIX") |
| 125 | + } |
| 126 | + defer os.Unsetenv("JUPYTERHUB_SERVICE_PREFIX") |
| 127 | + |
| 128 | + result := SubstitutePort(tt.command, tt.port) |
| 129 | + if len(result) != len(tt.expected) { |
| 130 | + t.Fatalf("SubstitutePort() returned %d args, want %d", len(result), len(tt.expected)) |
| 131 | + } |
| 132 | + for i := range result { |
| 133 | + if result[i] != tt.expected[i] { |
| 134 | + t.Errorf("SubstitutePort()[%d] = %q, want %q", i, result[i], tt.expected[i]) |
| 135 | + } |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
0 commit comments