Skip to content

Commit 84f4279

Browse files
committed
feat(sync): enhance dry run content comparison with detailed debugging
- Add comprehensive logging for content comparison in batch processor - Enhance repository file processing with size and match comparison - Add specific VSCode settings debugging when content differs - Update test to properly simulate existing target repo content - Fix testifylint issue by using suite.Require().Empty instead of Len - Improve error handling for missing existing file content
1 parent bbf7d7b commit 84f4279

3 files changed

Lines changed: 74 additions & 38 deletions

File tree

internal/sync/batch.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"strings"
910
"sync"
1011
"time"
1112

@@ -387,13 +388,37 @@ func (bp *BatchProcessor) processFileJobWithReporter(ctx context.Context, source
387388

388389
// Check if content actually changed (for existing files)
389390
existingContent, err := bp.getExistingFileContent(ctx, job.DestPath)
390-
if err == nil && string(existingContent) == string(transformedContent) {
391-
logger.Debug("File content unchanged after transformation, skipping")
392-
return fileProcessResult{
393-
Change: nil,
394-
Error: internalerrors.ErrTransformNotFound,
395-
Job: job,
391+
if err == nil {
392+
// Enhanced logging for content comparison
393+
existingStr := string(existingContent)
394+
transformedStr := string(transformedContent)
395+
contentMatches := existingStr == transformedStr
396+
397+
logger.WithFields(logrus.Fields{
398+
"existing_content_size": len(existingContent),
399+
"transformed_content_size": len(transformedContent),
400+
"content_matches": contentMatches,
401+
}).Debug("Comparing existing vs transformed content")
402+
403+
// For .vscode/settings.json specifically, add detailed content comparison when there's a mismatch
404+
if strings.Contains(job.DestPath, ".vscode/settings.json") && !contentMatches {
405+
logger.WithFields(logrus.Fields{
406+
"existing_content": existingStr,
407+
"transformed_content": transformedStr,
408+
"content_equal": contentMatches,
409+
}).Warn("VSCode settings content mismatch - displaying full content for debugging")
396410
}
411+
412+
if contentMatches {
413+
logger.Debug("File content unchanged after transformation, skipping")
414+
return fileProcessResult{
415+
Change: nil,
416+
Error: internalerrors.ErrTransformNotFound,
417+
Job: job,
418+
}
419+
}
420+
} else {
421+
logger.WithError(err).Debug("Could not get existing file content, treating as new file")
397422
}
398423

399424
// Create file change

internal/sync/directory_transform_test.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,8 +1143,16 @@ func (suite *DirectoryTransformTestSuite) TestVSCodeSettingsRealWorldTransformat
11431143

11441144
// Create a real engine with the actual repo transformer and a mock GitHub client
11451145
mockGHClient := gh.NewMockClient()
1146-
// Configure mock to return "file not found" for any GetFile call so all files are treated as new
1147-
mockGHClient.On("GetFile", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return((*gh.FileContent)(nil), ErrFileNotFound)
1146+
// Configure mock to return the target content for .vscode/settings.json
1147+
targetSettingsContent := strings.ReplaceAll(settingsContent, "github.com/mrz1836/go-broadcast", "github.com/mrz1836/go-pre-commit")
1148+
mockGHClient.On("GetFile", mock.Anything, "mrz1836/go-pre-commit", ".vscode/settings.json", "").Return(&gh.FileContent{
1149+
Content: []byte(targetSettingsContent),
1150+
}, nil)
1151+
1152+
// Return "file not found" for other files so they are treated as new
1153+
mockGHClient.On("GetFile", mock.Anything, mock.Anything, mock.MatchedBy(func(path string) bool {
1154+
return path != ".vscode/settings.json"
1155+
}), mock.Anything).Return((*gh.FileContent)(nil), ErrFileNotFound)
11481156

11491157
realEngine := &Engine{
11501158
transform: &DirectoryRealTransformChain{},
@@ -1157,33 +1165,10 @@ func (suite *DirectoryTransformTestSuite) TestVSCodeSettingsRealWorldTransformat
11571165
)
11581166

11591167
suite.Require().NoError(err)
1160-
suite.Require().Len(changes, 1, "Should have exactly one file change for settings.json")
1161-
1162-
change := changes[0]
1163-
suite.Equal(".vscode/settings.json", change.Path)
1164-
1165-
// Verify the transformation happened correctly
1166-
transformedContent := string(change.Content)
1167-
originalContent := string(change.OriginalContent)
11681168

1169-
// Key assertion: formatting.local should be transformed
1170-
suite.Contains(transformedContent, `"formatting.local": "github.com/mrz1836/go-pre-commit"`,
1171-
"Should transform formatting.local to target repo")
1172-
suite.NotContains(transformedContent, `"formatting.local": "github.com/mrz1836/go-broadcast"`,
1173-
"Should not contain original repo in formatting.local")
1169+
// Since the target repo already has the correct content, the file should be skipped
1170+
// and no changes should be returned
1171+
suite.Require().Empty(changes, "Should have no file changes since content matches after transformation")
11741172

1175-
// Verify the original content contained the source repo
1176-
suite.Contains(originalContent, `"formatting.local": "github.com/mrz1836/go-broadcast"`,
1177-
"Original content should contain source repo")
1178-
1179-
// Verify no other content was changed (except the transformation)
1180-
suite.Contains(transformedContent, `"go.useLanguageServer": true`)
1181-
suite.Contains(transformedContent, `"go.lintTool": "golangci-lint"`)
1182-
suite.Contains(transformedContent, `"formatting.gofumpt": true`)
1183-
1184-
suite.logger.WithFields(logrus.Fields{
1185-
"original_length": len(originalContent),
1186-
"transformed_length": len(transformedContent),
1187-
"file_path": change.Path,
1188-
}).Info("VSCode settings transformation test completed successfully")
1173+
suite.logger.Info("VSCode settings transformation test completed successfully - file correctly skipped due to identical content")
11891174
}

internal/sync/repository.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,35 @@ func (rs *RepositorySync) processFile(ctx context.Context, sourcePath string, fi
378378

379379
// Check if content actually changed (for existing files)
380380
existingContent, err := rs.getExistingFileContent(ctx, fileMapping.Dest)
381-
if err == nil && string(existingContent) == string(transformedContent) {
382-
rs.logger.WithField("file", fileMapping.Dest).Debug("File content unchanged, skipping")
383-
return nil, internalerrors.ErrTransformNotFound
381+
if err == nil {
382+
// Enhanced logging for content comparison
383+
existingStr := string(existingContent)
384+
transformedStr := string(transformedContent)
385+
contentMatches := existingStr == transformedStr
386+
387+
rs.logger.WithFields(logrus.Fields{
388+
"file": fileMapping.Dest,
389+
"existing_content_size": len(existingContent),
390+
"transformed_content_size": len(transformedContent),
391+
"content_matches": contentMatches,
392+
}).Debug("Comparing existing vs transformed content")
393+
394+
// For .vscode/settings.json specifically, add detailed content comparison when there's a mismatch
395+
if strings.Contains(fileMapping.Dest, ".vscode/settings.json") && !contentMatches {
396+
rs.logger.WithFields(logrus.Fields{
397+
"file": fileMapping.Dest,
398+
"existing_content": existingStr,
399+
"transformed_content": transformedStr,
400+
"content_equal": contentMatches,
401+
}).Warn("VSCode settings content mismatch - displaying full content for debugging")
402+
}
403+
404+
if contentMatches {
405+
rs.logger.WithField("file", fileMapping.Dest).Debug("File content unchanged, skipping")
406+
return nil, internalerrors.ErrTransformNotFound
407+
}
408+
} else {
409+
rs.logger.WithError(err).WithField("file", fileMapping.Dest).Debug("Could not get existing file content, treating as new file")
384410
}
385411

386412
return &FileChange{

0 commit comments

Comments
 (0)