Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 66 additions & 3 deletions internal/app/azldev/cmds/component/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ package component

import (
"fmt"
"path/filepath"

"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/components"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/sources"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/specs"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/workdir"
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
"github.com/microsoft/azure-linux-dev-tools/internal/providers/sourceproviders"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -58,6 +63,15 @@ type componentDetails struct {
specs.ComponentSpecDetails
}

type queryComponent struct {
components.Component
config projectconfig.ComponentConfig
}

func (c *queryComponent) GetConfig() *projectconfig.ComponentConfig {
return &c.config
}

// Queries env for component details, in accordance with options. Returns the found components.
func QueryComponents(
env *azldev.Env, options *QueryComponentsOptions,
Expand All @@ -74,9 +88,7 @@ func QueryComponents(
allDetails := make([]*componentDetails, 0, comps.Len())

for _, comp := range comps.Components() {
spec := comp.GetSpec()

specInfo, err := spec.Parse()
specInfo, err := parseComponentSpec(env, comp)
if err != nil {
return nil, fmt.Errorf("failed to parse spec for component %q:\n%w", comp.GetName(), err)
}
Expand All @@ -90,3 +102,54 @@ func QueryComponents(

return allDetails, nil
}

func parseComponentSpec(env *azldev.Env, comp components.Component) (*specs.ComponentSpecDetails, error) {
if comp.GetConfig().Spec.SourceType == projectconfig.SpecSourceTypeLocal {
return comp.GetSpec().Parse()
}

componentForPrep := comp
if comp.GetConfig().Spec.SourceType == projectconfig.SpecSourceTypeUnspecified {
normalizedConfig := *comp.GetConfig()
normalizedConfig.Spec.SourceType = projectconfig.SpecSourceTypeUpstream
componentForPrep = &queryComponent{
Component: comp,
config: normalizedConfig,
}
}

distro, err := sourceproviders.ResolveDistro(env, componentForPrep)
if err != nil {
return nil, fmt.Errorf("failed to resolve distro for component %q:\n%w", comp.GetName(), err)
}

sourceManager, err := sourceproviders.NewSourceManager(env, distro)
if err != nil {
return nil, fmt.Errorf("failed to create source manager:\n%w", err)
}

preparer, err := sources.NewPreparer(sourceManager, env.FS(), env, env, sources.WithSkipLookaside())
if err != nil {
return nil, fmt.Errorf("failed to create source preparer:\n%w", err)
}

workDirFactory, err := workdir.NewFactory(env.FS(), env.WorkDir(), env.ConstructionTime())
if err != nil {
return nil, fmt.Errorf("failed to create work dir factory:\n%w", err)
}

preparedSourcesDir, err := workDirFactory.Create(comp.GetName(), "query-spec")
if err != nil {
return nil, fmt.Errorf("failed to create work dir for component %#q:\n%w", comp.GetName(), err)
}

if err := preparer.PrepareSources(env, componentForPrep, preparedSourcesDir, true /* applyOverlays */); err != nil {
return nil, fmt.Errorf("failed to prepare sources for component %#q:\n%w", comp.GetName(), err)
Comment on lines +131 to +147
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseComponentSpec prepares upstream sources without enabling dist-git / synthetic history (sources.WithGitRepo). In other paths (e.g., component render) WithGitRepo is used so rpmautospec can expand %autorelease / %autochangelog correctly and to keep release numbering consistent with overlay commit count. As written, component query may produce incorrect release/changelog values (or fail) for specs that rely on rpmautospec because the upstream .git dir is removed and no synthetic commits are created. Consider aligning this with render by adding sources.WithGitRepo(env.Config().Project.DefaultAuthorEmail) (or otherwise preserving .git + synthetic history) for this query-only prep path, or explicitly disabling rpmautospec-dependent expansion and documenting the limitation.

Copilot uses AI. Check for mistakes.
}

preparedConfig := *componentForPrep.GetConfig()
preparedConfig.Spec.SourceType = projectconfig.SpecSourceTypeLocal
preparedConfig.Spec.Path = filepath.Join(preparedSourcesDir, comp.GetName()+".spec")

return specs.NewSpec(env, preparedConfig).Parse()
}
83 changes: 83 additions & 0 deletions internal/app/azldev/cmds/component/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,86 @@ func TestQueryComponents_OneComponent(t *testing.T) {
result := results[0]
assert.Equal(t, testComponentName, result.Name)
}

func TestQueryComponents_UpstreamComponent(t *testing.T) {
const testComponentName = "test-component"
const testUpstreamName = "test-component-upstream"

testCases := []struct {
name string
component projectconfig.ComponentConfig
specFileName string
specNameField string
}{
{
name: "default spec source type uses normalized component name",
component: projectconfig.ComponentConfig{
Name: testComponentName,
},
specFileName: testComponentName,
specNameField: testComponentName,
},
{
name: "explicit upstream spec source type uses upstream name",
component: projectconfig.ComponentConfig{
Name: testComponentName,
Spec: projectconfig.SpecConfig{
SourceType: projectconfig.SpecSourceTypeUpstream,
UpstreamName: testUpstreamName,
},
},
Comment on lines +104 to +110
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

projectconfig.SpecConfig does not exist (the ComponentConfig.Spec field is projectconfig.SpecSource). This won’t compile; use the same spec struct type as in TestQueryComponents_OneComponent when setting SourceType/UpstreamName.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

specFileName: testUpstreamName,
specNameField: testUpstreamName,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
testEnv := testutils.NewTestEnv(t)
testEnv.Config.Components[testComponentName] = testCase.component

testEnv.CmdFactory.RegisterCommandInSearchPath(mock.MockBinary)

testEnv.CmdFactory.RunHandler = func(cmd *exec.Cmd) error {
if len(cmd.Args) >= 2 && cmd.Args[0] == "git" && cmd.Args[1] == "clone" {
cloneDir := cmd.Args[len(cmd.Args)-1]
specPath := cloneDir + "/" + testCase.specFileName + ".spec"

return fileutils.WriteFile(
testEnv.FS(),
specPath,
[]byte("Name: "+testCase.specNameField+"\nVersion: 1.0.0\n"),
fileperms.PublicFile,
)
}

return nil
}

testEnv.CmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) {
if len(cmd.Args) >= 5 &&
cmd.Args[0] == "git" &&
cmd.Args[1] == "-C" &&
cmd.Args[3] == "rev-parse" &&
cmd.Args[4] == "HEAD" {
return "head123abc\n", nil
}

return "name=test-component\nepoch=0\nversion=1.0.0\nrelease=1.azl3\n", nil
}
Comment on lines +139 to +149
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test writes different Name: values into the generated .spec, but the mocked rpmspec output is hard-coded to name=test-component and the assertions don’t check the returned spec name/version fields. As written, the test won’t fail if the query path parses the wrong spec (or never reads it). Consider making the mock output depend on the test case and asserting on result.Name (and/or other parsed fields) to actually validate upstream vs default behavior.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback


options := component.QueryComponentsOptions{
ComponentFilter: components.ComponentFilter{
ComponentNamePatterns: []string{testComponentName},
},
}

results, err := component.QueryComponents(testEnv.Env, &options)
require.NoError(t, err)
require.Len(t, results, 1)

result := results[0]
assert.Equal(t, testComponentName, result.Name)
})
}
}
Loading