|
8 | 8 | "path/filepath" |
9 | 9 | "testing" |
10 | 10 |
|
| 11 | + "github.com/azure/azure-dev/cli/azd/internal/tracing" |
| 12 | + "github.com/azure/azure-dev/cli/azd/internal/tracing/fields" |
11 | 13 | "github.com/azure/azure-dev/cli/azd/pkg/environment" |
12 | 14 | "github.com/azure/azure-dev/cli/azd/pkg/exec" |
13 | 15 | "github.com/azure/azure-dev/cli/azd/pkg/ext" |
@@ -152,3 +154,76 @@ func Test_HooksRunAction_FiltersLayerHooks(t *testing.T) { |
152 | 154 | filepath.Join(projectPath, "infra/shared"), |
153 | 155 | }, gotCwds) |
154 | 156 | } |
| 157 | + |
| 158 | +func Test_HooksRunAction_SetsTelemetryTypeForLayer(t *testing.T) { |
| 159 | + mockContext := mocks.NewMockContext(context.Background()) |
| 160 | + env := environment.NewWithValues("test", nil) |
| 161 | + envManager := &mockenv.MockEnvManager{} |
| 162 | + envManager.On("Reload", mock.Anything, mock.Anything).Return(nil) |
| 163 | + |
| 164 | + t.Cleanup(func() { |
| 165 | + tracing.SetUsageAttributes() |
| 166 | + }) |
| 167 | + tracing.SetUsageAttributes() |
| 168 | + |
| 169 | + projectConfig := &project.ProjectConfig{ |
| 170 | + Name: "test", |
| 171 | + Path: t.TempDir(), |
| 172 | + Services: map[string]*project.ServiceConfig{}, |
| 173 | + Infra: provisioning.Options{ |
| 174 | + Layers: []provisioning.Options{ |
| 175 | + { |
| 176 | + Name: "core", |
| 177 | + Path: "infra/core", |
| 178 | + Hooks: provisioning.HooksConfig{ |
| 179 | + "preprovision": {{ |
| 180 | + Shell: ext.ShellTypeBash, |
| 181 | + Run: "echo core", |
| 182 | + }}, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + } |
| 188 | + |
| 189 | + mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool { |
| 190 | + return true |
| 191 | + }).Respond(exec.NewRunResult(0, "", "")) |
| 192 | + |
| 193 | + action := &hooksRunAction{ |
| 194 | + projectConfig: projectConfig, |
| 195 | + env: env, |
| 196 | + envManager: envManager, |
| 197 | + importManager: project.NewImportManager(nil), |
| 198 | + commandRunner: mockContext.CommandRunner, |
| 199 | + console: mockContext.Console, |
| 200 | + flags: &hooksRunFlags{layer: "core"}, |
| 201 | + args: []string{"preprovision"}, |
| 202 | + serviceLocator: mockContext.Container, |
| 203 | + } |
| 204 | + |
| 205 | + _, err := action.Run(*mockContext.Context) |
| 206 | + require.NoError(t, err) |
| 207 | + |
| 208 | + var hookType string |
| 209 | + for _, attr := range tracing.GetUsageAttributes() { |
| 210 | + if attr.Key == fields.HooksTypeKey.Key { |
| 211 | + hookType = attr.Value.AsString() |
| 212 | + break |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + require.Equal(t, "layer", hookType) |
| 217 | +} |
| 218 | + |
| 219 | +func Test_HooksRunAction_RejectsServiceAndLayerTogether(t *testing.T) { |
| 220 | + action := &hooksRunAction{ |
| 221 | + env: environment.NewWithValues("test", nil), |
| 222 | + flags: &hooksRunFlags{service: "api", layer: "core"}, |
| 223 | + args: []string{"preprovision"}, |
| 224 | + } |
| 225 | + |
| 226 | + _, err := action.Run(context.Background()) |
| 227 | + require.Error(t, err) |
| 228 | + require.ErrorContains(t, err, "--service and --layer cannot be used together") |
| 229 | +} |
0 commit comments