|
5 | 5 | "os" |
6 | 6 | "os/exec" |
7 | 7 | "path" |
| 8 | + "path/filepath" |
8 | 9 | "runtime" |
9 | 10 | "slices" |
10 | 11 | "strings" |
@@ -87,6 +88,7 @@ var dapBuildTests = []func(t *testing.T, sb integration.Sandbox){ |
87 | 88 | testDapBuildStepOut, |
88 | 89 | testDapBuildVariables, |
89 | 90 | testDapBuildDeferredEval, |
| 91 | + testDapBuildExitedEvent, |
90 | 92 | } |
91 | 93 |
|
92 | 94 | func testDapBuild(t *testing.T, sb integration.Sandbox) { |
@@ -914,6 +916,73 @@ func testDapBuildDeferredEval(t *testing.T, sb integration.Sandbox) { |
914 | 916 | require.ErrorAs(t, done(true), &exitErr) |
915 | 917 | } |
916 | 918 |
|
| 919 | +func testDapBuildExitedEvent(t *testing.T, sb integration.Sandbox) { |
| 920 | + t.Run("success", func(t *testing.T) { |
| 921 | + dir := createTestProject(t) |
| 922 | + client, done, err := dapBuildCmd(t, sb) |
| 923 | + require.NoError(t, err) |
| 924 | + |
| 925 | + ch := make(chan *dap.ExitedEvent, 1) |
| 926 | + client.RegisterEvent("exited", func(em dap.EventMessage) { |
| 927 | + ch <- em.(*dap.ExitedEvent) |
| 928 | + close(ch) |
| 929 | + }) |
| 930 | + |
| 931 | + // Project should just build normally. |
| 932 | + doLaunch(t, client, commands.LaunchConfig{ |
| 933 | + Dockerfile: path.Join(dir, "Dockerfile"), |
| 934 | + ContextPath: dir, |
| 935 | + }) |
| 936 | + |
| 937 | + select { |
| 938 | + case exited := <-ch: |
| 939 | + require.Equal(t, 0, exited.Body.ExitCode) |
| 940 | + case <-time.After(5 * time.Second): |
| 941 | + require.Fail(t, "timeout reached") |
| 942 | + } |
| 943 | + |
| 944 | + require.NoError(t, done(true)) |
| 945 | + }) |
| 946 | + |
| 947 | + t.Run("failure", func(t *testing.T) { |
| 948 | + dir := createTestProject(t) |
| 949 | + client, done, err := dapBuildCmd(t, sb) |
| 950 | + require.NoError(t, err) |
| 951 | + |
| 952 | + ch := make(chan *dap.ExitedEvent, 1) |
| 953 | + client.RegisterEvent("exited", func(em dap.EventMessage) { |
| 954 | + ch <- em.(*dap.ExitedEvent) |
| 955 | + close(ch) |
| 956 | + }) |
| 957 | + |
| 958 | + // Delete foo from the test project so this will fail. |
| 959 | + err = os.Remove(filepath.Join(dir, "foo")) |
| 960 | + require.NoError(t, err) |
| 961 | + |
| 962 | + interruptCh := pollInterruptEvents(client) |
| 963 | + doLaunch(t, client, commands.LaunchConfig{ |
| 964 | + Dockerfile: path.Join(dir, "Dockerfile"), |
| 965 | + ContextPath: dir, |
| 966 | + }) |
| 967 | + |
| 968 | + // We will hit an interrupt because of the failure. |
| 969 | + stopped := waitForInterrupt[*dap.StoppedEvent](t, interruptCh) |
| 970 | + require.Equal(t, "exception", stopped.Body.Reason) |
| 971 | + |
| 972 | + // Continue execution which should trigger the exited event. |
| 973 | + doNext(t, client, stopped.Body.ThreadId) |
| 974 | + select { |
| 975 | + case exited := <-ch: |
| 976 | + require.NotEqual(t, 0, exited.Body.ExitCode) |
| 977 | + case <-time.After(time.Second): |
| 978 | + require.Fail(t, "timeout reached") |
| 979 | + } |
| 980 | + |
| 981 | + var exitErr *exec.ExitError |
| 982 | + require.ErrorAs(t, done(false), &exitErr) |
| 983 | + }) |
| 984 | +} |
| 985 | + |
917 | 986 | func doLaunch(t *testing.T, client *daptest.Client, config commands.LaunchConfig, bps ...dap.SourceBreakpoint) { |
918 | 987 | t.Helper() |
919 | 988 |
|
|
0 commit comments