Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
23df19763f |
@@ -1339,36 +1339,6 @@ func TestGenerateDefaultOutput(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestGenerateSeveralPathsDefaultOutput runs gen without --output on two
|
|
||||||
// directories: it writes index.mf in the current directory, listing both
|
|
||||||
// directories' files, and writes no index.mf in either directory.
|
|
||||||
//
|
|
||||||
//nolint:paralleltest // changes the process-global working directory
|
|
||||||
func TestGenerateSeveralPathsDefaultOutput(t *testing.T) {
|
|
||||||
root := t.TempDir()
|
|
||||||
fs := afero.NewOsFs()
|
|
||||||
dirs := []string{"first", "second"}
|
|
||||||
|
|
||||||
for _, dir := range dirs {
|
|
||||||
require.NoError(t, fs.MkdirAll(filepath.Join(root, dir), 0o750))
|
|
||||||
writeTestFile(t, fs, filepath.Join(root, dir, dir+".txt"), dir)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Chdir(root)
|
|
||||||
|
|
||||||
opts := testOpts(append([]string{testApp, cmdGenerate, "-q"}, dirs...), fs)
|
|
||||||
require.Equal(t, 0, runCLI(opts), "stderr: %s", testStderr(t, opts))
|
|
||||||
|
|
||||||
assert.ElementsMatch(t, []string{"first.txt", "second.txt"},
|
|
||||||
manifestPaths(t, fs, filepath.Join(root, defaultManifestName)))
|
|
||||||
|
|
||||||
for _, dir := range dirs {
|
|
||||||
exists, err := afero.Exists(fs, filepath.Join(root, dir, defaultManifestName))
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.False(t, exists, "index.mf written in %s", dir)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestGenerateBytesDoNotDependOnOutput runs gen --seed on one tree, each
|
// TestGenerateBytesDoNotDependOnOutput runs gen --seed on one tree, each
|
||||||
// time writing to another file, over a file already there and beside an
|
// time writing to another file, over a file already there and beside an
|
||||||
// earlier run's temp file: neither is listed, and what is listed depends
|
// earlier run's temp file: neither is listed, and what is listed depends
|
||||||
@@ -1440,29 +1410,6 @@ func TestGenerateRefusesExistingDefaultOutput(t *testing.T) {
|
|||||||
assert.Equal(t, "previous manifest", string(content))
|
assert.Equal(t, "previous manifest", string(content))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestGenerateRefusesEmptyOutput runs gen with --force and an --output
|
|
||||||
// given an empty value, as an unset shell variable gives it, on a
|
|
||||||
// directory already holding index.mf: gen fails and leaves that file as it
|
|
||||||
// was.
|
|
||||||
func TestGenerateRefusesEmptyOutput(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
output := filepath.Join(testDir, defaultManifestName)
|
|
||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
require.NoError(t, fs.MkdirAll(testDir, 0o755))
|
|
||||||
writeTestFile(t, fs, testFile1, "hello")
|
|
||||||
writeTestFile(t, fs, output, "previous manifest")
|
|
||||||
|
|
||||||
opts := testOpts([]string{testApp, cmdGenerate, "-q", "-f", "-o", "", testDir}, fs)
|
|
||||||
assert.Equal(t, 1, runCLI(opts))
|
|
||||||
assert.Contains(t, testStderr(t, opts), errEmptyOutput.Error())
|
|
||||||
|
|
||||||
content, err := afero.ReadFile(fs, output)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, "previous manifest", string(content))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenerateAtomicWriteUsesTemp(t *testing.T) {
|
func TestGenerateAtomicWriteUsesTemp(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
+29
-53
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -27,8 +26,6 @@ var (
|
|||||||
// rendered message stays exactly as mfer has always printed it.
|
// rendered message stays exactly as mfer has always printed it.
|
||||||
errOutputExists = errors.New(
|
errOutputExists = errors.New(
|
||||||
"already exists (use --force to overwrite)")
|
"already exists (use --force to overwrite)")
|
||||||
// errEmptyOutput indicates --output given with an empty value.
|
|
||||||
errEmptyOutput = errors.New("--output must not be empty")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// reportEnumProgress renders enumeration progress until the channel
|
// reportEnumProgress renders enumeration progress until the channel
|
||||||
@@ -93,20 +90,14 @@ func (mfa *CLIApp) collectInputPaths(args cli.Args) ([]string, error) {
|
|||||||
|
|
||||||
// outputPath returns the file gen writes the manifest to: the one --output
|
// outputPath returns the file gen writes the manifest to: the one --output
|
||||||
// names, or else index.mf in the directory the only argument names, or
|
// names, or else index.mf in the directory the only argument names, or
|
||||||
// beside the file it names, or else in the current directory. An --output
|
// beside the file it names, or else in the current directory.
|
||||||
// given with an empty value is refused.
|
func (mfa *CLIApp) outputPath(cmd *cli.Command) string {
|
||||||
func (mfa *CLIApp) outputPath(cmd *cli.Command) (string, error) {
|
if output := cmd.String("output"); output != "" {
|
||||||
if cmd.IsSet("output") {
|
return output
|
||||||
output := cmd.String("output")
|
|
||||||
if output == "" {
|
|
||||||
return "", errEmptyOutput
|
|
||||||
}
|
|
||||||
|
|
||||||
return output, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Args().Len() != 1 {
|
if cmd.Args().Len() != 1 {
|
||||||
return defaultManifestName, nil
|
return defaultManifestName
|
||||||
}
|
}
|
||||||
|
|
||||||
arg := cmd.Args().First()
|
arg := cmd.Args().First()
|
||||||
@@ -114,10 +105,10 @@ func (mfa *CLIApp) outputPath(cmd *cli.Command) (string, error) {
|
|||||||
// A path that does not exist is refused when it is enumerated.
|
// A path that does not exist is refused when it is enumerated.
|
||||||
info, err := mfa.Fs.Stat(arg)
|
info, err := mfa.Fs.Stat(arg)
|
||||||
if err == nil && !info.IsDir() {
|
if err == nil && !info.IsDir() {
|
||||||
return filepath.Join(filepath.Dir(arg), defaultManifestName), nil
|
return filepath.Join(filepath.Dir(arg), defaultManifestName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return filepath.Join(arg, defaultManifestName), nil
|
return filepath.Join(arg, defaultManifestName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildScannerOptions constructs scanner options from the CLI flags and
|
// buildScannerOptions constructs scanner options from the CLI flags and
|
||||||
@@ -236,53 +227,22 @@ func (mfa *CLIApp) runEnumeratePhase(cmd *cli.Command, s *mfer.Scanner) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// runScanPhase reads the enumerated files and writes the manifest to out,
|
|
||||||
// with optional progress reporting.
|
|
||||||
func (mfa *CLIApp) runScanPhase(
|
|
||||||
ctx context.Context, cmd *cli.Command, s *mfer.Scanner, out io.Writer,
|
|
||||||
) error {
|
|
||||||
var (
|
|
||||||
scanProgress chan mfer.ScanStatus
|
|
||||||
scanWg sync.WaitGroup
|
|
||||||
)
|
|
||||||
|
|
||||||
if cmd.Bool("progress") {
|
|
||||||
scanProgress = make(chan mfer.ScanStatus, 1)
|
|
||||||
|
|
||||||
scanWg.Add(1)
|
|
||||||
|
|
||||||
go reportScanProgress(scanProgress, &scanWg)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := s.ToManifest(ctx, out, scanProgress)
|
|
||||||
|
|
||||||
scanWg.Wait()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("generate manifest: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mfa *CLIApp) generateManifestOperation(
|
func (mfa *CLIApp) generateManifestOperation(
|
||||||
ctx context.Context, cmd *cli.Command,
|
ctx context.Context, cmd *cli.Command,
|
||||||
) error {
|
) error {
|
||||||
log.Debug("generateManifestOperation()")
|
log.Debug("generateManifestOperation()")
|
||||||
|
|
||||||
outputPath, err := mfa.outputPath(cmd)
|
outputPath := mfa.outputPath(cmd)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
s := mfer.NewScannerWithOptions(mfa.buildScannerOptions(cmd, outputPath))
|
s := mfer.NewScannerWithOptions(mfa.buildScannerOptions(cmd, outputPath))
|
||||||
|
|
||||||
// Phase 1: Enumeration - collect paths and stat files
|
// Phase 1: Enumeration - collect paths and stat files
|
||||||
err = mfa.runEnumeratePhase(cmd, s)
|
err := mfa.runEnumeratePhase(cmd, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showProgress := cmd.Bool("progress")
|
||||||
|
|
||||||
// Check if output file exists
|
// Check if output file exists
|
||||||
if exists, _ := afero.Exists(mfa.Fs, outputPath); exists && !cmd.Bool("force") {
|
if exists, _ := afero.Exists(mfa.Fs, outputPath); exists && !cmd.Bool("force") {
|
||||||
return fmt.Errorf("output file %s %w", outputPath, errOutputExists)
|
return fmt.Errorf("output file %s %w", outputPath, errOutputExists)
|
||||||
@@ -314,9 +274,25 @@ func (mfa *CLIApp) generateManifestOperation(
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Phase 2: Scan - read file contents and generate manifest
|
// Phase 2: Scan - read file contents and generate manifest
|
||||||
err = mfa.runScanPhase(ctx, cmd, s, outFile)
|
var (
|
||||||
|
scanProgress chan mfer.ScanStatus
|
||||||
|
scanWg sync.WaitGroup
|
||||||
|
)
|
||||||
|
|
||||||
|
if showProgress {
|
||||||
|
scanProgress = make(chan mfer.ScanStatus, 1)
|
||||||
|
|
||||||
|
scanWg.Add(1)
|
||||||
|
|
||||||
|
go reportScanProgress(scanProgress, &scanWg)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.ToManifest(ctx, outFile, scanProgress)
|
||||||
|
|
||||||
|
scanWg.Wait()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("generate manifest: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close file before rename to ensure all data is flushed
|
// Close file before rename to ensure all data is flushed
|
||||||
|
|||||||
Reference in New Issue
Block a user