dap: reduce branching on onLaunchRequest mode validations (#2364)

This commit is contained in:
Luis Gabriel Gomez 2021-03-05 06:07:23 -03:00 committed by GitHub
parent 314ae669a3
commit 4364c728f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 14 deletions

@ -432,8 +432,19 @@ func (s *Server) onInitializeRequest(request *dap.InitializeRequest) {
const debugBinary string = "./__debug_bin" const debugBinary string = "./__debug_bin"
func (s *Server) onLaunchRequest(request *dap.LaunchRequest) { func (s *Server) onLaunchRequest(request *dap.LaunchRequest) {
// TODO(polina): Respond with an error if debug session is in progress? // Validate launch request mode
mode, ok := request.Arguments["mode"]
if !ok || mode == "" {
mode = "debug"
}
if !isValidLaunchMode(mode) {
s.sendErrorResponse(request.Request,
FailedToLaunch, "Failed to launch",
fmt.Sprintf("Unsupported 'mode' value %q in debug configuration.", mode))
return
}
// TODO(polina): Respond with an error if debug session is in progress?
program, ok := request.Arguments["program"].(string) program, ok := request.Arguments["program"].(string)
if !ok || program == "" { if !ok || program == "" {
s.sendErrorResponse(request.Request, s.sendErrorResponse(request.Request,
@ -442,11 +453,6 @@ func (s *Server) onLaunchRequest(request *dap.LaunchRequest) {
return return
} }
mode, ok := request.Arguments["mode"]
if !ok || mode == "" {
mode = "debug"
}
if mode == "debug" || mode == "test" { if mode == "debug" || mode == "test" {
output, ok := request.Arguments["output"].(string) output, ok := request.Arguments["output"].(string)
if !ok || output == "" { if !ok || output == "" {
@ -486,14 +492,6 @@ func (s *Server) onLaunchRequest(request *dap.LaunchRequest) {
s.binaryToRemove = debugname s.binaryToRemove = debugname
} }
// TODO(polina): support "remote" mode
if mode != "exec" && mode != "debug" && mode != "test" {
s.sendErrorResponse(request.Request,
FailedToLaunch, "Failed to launch",
fmt.Sprintf("Unsupported 'mode' value %q in debug configuration.", mode))
return
}
s.setLaunchAttachArgs(request) s.setLaunchAttachArgs(request)
var targetArgs []string var targetArgs []string
@ -535,6 +533,16 @@ func (s *Server) onLaunchRequest(request *dap.LaunchRequest) {
s.send(&dap.LaunchResponse{Response: *newResponse(request.Request)}) s.send(&dap.LaunchResponse{Response: *newResponse(request.Request)})
} }
// TODO(polina): support "remote" mode
func isValidLaunchMode(launchMode interface{}) bool {
switch launchMode {
case "exec", "debug", "test":
return true
}
return false
}
// onDisconnectRequest handles the DisconnectRequest. Per the DAP spec, // onDisconnectRequest handles the DisconnectRequest. Per the DAP spec,
// it disconnects the debuggee and signals that the debug adaptor // it disconnects the debuggee and signals that the debug adaptor
// (in our case this TCP server) can be terminated. // (in our case this TCP server) can be terminated.

@ -2559,6 +2559,10 @@ func TestBadLaunchRequests(t *testing.T) {
client.LaunchRequestWithArgs(map[string]interface{}{"mode": "debug", "program": fixture.Source, "buildFlags": "123"}) client.LaunchRequestWithArgs(map[string]interface{}{"mode": "debug", "program": fixture.Source, "buildFlags": "123"})
expectFailedToLaunch(client.ExpectErrorResponse(t)) // Build error expectFailedToLaunch(client.ExpectErrorResponse(t)) // Build error
client.LaunchRequest("", fixture.Path, stopOnEntry)
expectFailedToLaunchWithMessage(client.ExpectErrorResponse(t),
"Failed to launch: Build error: exit status 1")
// We failed to launch the program. Make sure shutdown still works. // We failed to launch the program. Make sure shutdown still works.
client.DisconnectRequest() client.DisconnectRequest()
dresp := client.ExpectDisconnectResponse(t) dresp := client.ExpectDisconnectResponse(t)