Skip to content

Commit 6a6a998

Browse files
author
Antonio Salinas
committed
fix: get pkg name from .spec
1 parent 0726db2 commit 6a6a998

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

internal/app/azldev/cmds/downloadsources/downloadsources.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import (
77
"fmt"
88
"log/slog"
99
"path/filepath"
10+
"strings"
1011

12+
"github.com/bmatcuk/doublestar/v4"
1113
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev"
1214
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/components"
1315
"github.com/microsoft/azure-linux-dev-tools/internal/providers/sourceproviders"
1416
"github.com/microsoft/azure-linux-dev-tools/internal/providers/sourceproviders/fedorasource"
1517
"github.com/microsoft/azure-linux-dev-tools/internal/utils/downloader"
18+
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
1619
"github.com/microsoft/azure-linux-dev-tools/internal/utils/retry"
1720
"github.com/spf13/cobra"
1821
)
@@ -190,7 +193,7 @@ func resolveDownloadParams(
190193
) (packageName string, lookasideBaseURIs []string, err error) {
191194
// Standalone mode: --lookaside-uri provided.
192195
if len(options.LookasideBaseURIs) > 0 {
193-
packageName, err = resolvePackageNameFromDir(options)
196+
packageName, err = resolvePackageNameFromDir(env, options)
194197
if err != nil {
195198
return "", nil, err
196199
}
@@ -202,16 +205,40 @@ func resolveDownloadParams(
202205
return resolveFromComponent(env, options)
203206
}
204207

205-
// resolvePackageNameFromDir derives the package name from the directory basename.
206-
func resolvePackageNameFromDir(options *DownloadSourcesOptions) (string, error) {
207-
absDir, err := filepath.Abs(options.Directory)
208+
// resolvePackageNameFromDir derives the package name from a .spec file
209+
// in the directory. Exactly one .spec file must be present.
210+
func resolvePackageNameFromDir(
211+
env *azldev.Env, options *DownloadSourcesOptions,
212+
) (string, error) {
213+
specPattern := filepath.Join(options.Directory, "*.spec")
214+
215+
specFiles, err := fileutils.Glob(
216+
env.FS(), specPattern, doublestar.WithFilesOnly(),
217+
)
208218
if err != nil {
209-
return "", fmt.Errorf("failed to resolve absolute path for %#q:\n%w", options.Directory, err)
219+
return "", fmt.Errorf(
220+
"failed to search for spec files in %#q:\n%w",
221+
options.Directory, err)
222+
}
223+
224+
if len(specFiles) == 0 {
225+
return "", fmt.Errorf(
226+
"no .spec file found in %#q to derive package name",
227+
options.Directory)
228+
}
229+
230+
if len(specFiles) > 1 {
231+
return "", fmt.Errorf(
232+
"multiple .spec files found in %#q; cannot determine package name",
233+
options.Directory)
210234
}
211235

212-
packageName := filepath.Base(absDir)
236+
packageName := strings.TrimSuffix(
237+
filepath.Base(specFiles[0]), ".spec",
238+
)
213239

214-
slog.Debug("Derived package name from directory name", "name", packageName, "dir", options.Directory)
240+
slog.Debug("Derived package name from spec file",
241+
"name", packageName, "specFile", specFiles[0])
215242

216243
return packageName, nil
217244
}

internal/app/azldev/cmds/downloadsources/downloadsources_test.go

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package downloadsources_test
55

66
import (
7-
"errors"
87
"testing"
98

109
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev"
@@ -22,6 +21,8 @@ import (
2221

2322
const testLookasideURI = "https://example.com/lookaside/$pkg/$filename/$hashtype/$hash/$filename"
2423

24+
const testPkgDir = "/project/curl"
25+
2526
func TestOnAppInit(t *testing.T) {
2627
ctrl := gomock.NewController(t)
2728
app := azldev.NewApp(opctx_test.NewMockFileSystemFactory(ctrl), opctx_test.NewMockOSEnvFactory(ctrl))
@@ -63,64 +64,56 @@ func TestDownloadSources_StandaloneMode(t *testing.T) {
6364
testEnv := testutils.NewTestEnv(t)
6465
ctrl := gomock.NewController(t)
6566

66-
pkgDir := "/project/curl"
67-
require.NoError(t, fileutils.MkdirAll(testEnv.TestFS, pkgDir))
68-
require.NoError(t, fileutils.WriteFile(testEnv.TestFS, pkgDir+"/sources", []byte(""), fileperms.PrivateFile))
67+
require.NoError(t, fileutils.MkdirAll(testEnv.TestFS, testPkgDir))
68+
require.NoError(t, fileutils.WriteFile(testEnv.TestFS, testPkgDir+"/curl.spec", []byte(""), fileperms.PrivateFile))
69+
require.NoError(t, fileutils.WriteFile(testEnv.TestFS, testPkgDir+"/sources", []byte(""), fileperms.PrivateFile))
6970

7071
mockDownloader := fedorasource_test.NewMockFedoraSourceDownloader(ctrl)
7172
mockDownloader.EXPECT().
72-
ExtractSourcesFromRepo(gomock.Any(), pkgDir, "curl", testLookasideURI, gomock.Any(), gomock.Any()).
73+
ExtractSourcesFromRepo(gomock.Any(), testPkgDir, "curl", testLookasideURI, gomock.Any(), gomock.Any()).
7374
Return(nil)
7475

7576
options := &downloadsources.DownloadSourcesOptions{
76-
Directory: pkgDir,
77+
Directory: testPkgDir,
7778
LookasideBaseURIs: []string{testLookasideURI},
7879
LookasideDownloader: mockDownloader,
7980
}
8081

81-
// Package name "curl" derived from directory basename.
82+
// Package name "curl" derived from curl.spec filename.
8283
err := downloadsources.DownloadSources(testEnv.Env, options)
8384
require.NoError(t, err)
8485
}
8586

86-
func TestDownloadSources_StandaloneMode_NonexistentDir(t *testing.T) {
87+
func TestDownloadSources_StandaloneMode_NoSpecFile(t *testing.T) {
8788
testEnv := testutils.NewTestEnv(t)
88-
ctrl := gomock.NewController(t)
8989

90-
mockDownloader := fedorasource_test.NewMockFedoraSourceDownloader(ctrl)
91-
mockDownloader.EXPECT().
92-
ExtractSourcesFromRepo(
93-
gomock.Any(), "/project/nonexistent", "nonexistent",
94-
testLookasideURI, gomock.Any(), gomock.Any(),
95-
).
96-
Return(errors.New("repository directory does not exist"))
90+
require.NoError(t, fileutils.MkdirAll(testEnv.TestFS, testPkgDir))
9791

9892
options := &downloadsources.DownloadSourcesOptions{
99-
Directory: "/project/nonexistent",
100-
LookasideBaseURIs: []string{testLookasideURI},
101-
LookasideDownloader: mockDownloader,
93+
Directory: testPkgDir,
94+
LookasideBaseURIs: []string{testLookasideURI},
10295
}
10396

10497
err := downloadsources.DownloadSources(testEnv.Env, options)
10598
require.Error(t, err)
106-
assert.Contains(t, err.Error(), "does not exist")
99+
assert.Contains(t, err.Error(), "no .spec file found")
107100
}
108101

109102
func TestDownloadSources_StandaloneMode_NoSourcesFile(t *testing.T) {
110103
testEnv := testutils.NewTestEnv(t)
111104
ctrl := gomock.NewController(t)
112105

113-
pkgDir := "/project/curl"
114-
require.NoError(t, fileutils.MkdirAll(testEnv.TestFS, pkgDir))
106+
require.NoError(t, fileutils.MkdirAll(testEnv.TestFS, testPkgDir))
107+
require.NoError(t, fileutils.WriteFile(testEnv.TestFS, testPkgDir+"/curl.spec", []byte(""), fileperms.PrivateFile))
115108

116109
// ExtractSourcesFromRepo returns nil when no sources file exists.
117110
mockDownloader := fedorasource_test.NewMockFedoraSourceDownloader(ctrl)
118111
mockDownloader.EXPECT().
119-
ExtractSourcesFromRepo(gomock.Any(), pkgDir, "curl", testLookasideURI, gomock.Any(), gomock.Any()).
112+
ExtractSourcesFromRepo(gomock.Any(), testPkgDir, "curl", testLookasideURI, gomock.Any(), gomock.Any()).
120113
Return(nil)
121114

122115
options := &downloadsources.DownloadSourcesOptions{
123-
Directory: pkgDir,
116+
Directory: testPkgDir,
124117
LookasideBaseURIs: []string{testLookasideURI},
125118
LookasideDownloader: mockDownloader,
126119
}

0 commit comments

Comments
 (0)