From 198eefcf39aa3f07e2f676bdf35ccd64d90e66c9 Mon Sep 17 00:00:00 2001
From: shreyas-goenka <88374338+shreyas-goenka@users.noreply.github.com>
Date: Thu, 22 Dec 2022 11:35:32 +0100
Subject: [PATCH] Fix folder syncing on windows (#149)
Tested by running the unit and integration tests locally
Tested manually on windows
Screenshot from windows sync logs indicating that the correct slashed
for paths were used:
@pietern with this the state machine for syncing becomes slightly more
complicated, indicating a stronger need for a tree based approach herre
Co-authored-by: Pieter Noordhuis
---
cmd/sync/snapshot.go | 15 +++++++++++----
cmd/sync/snapshot_test.go | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+), 4 deletions(-)
diff --git a/cmd/sync/snapshot.go b/cmd/sync/snapshot.go
index 9ab3f445..54a7e29a 100644
--- a/cmd/sync/snapshot.go
+++ b/cmd/sync/snapshot.go
@@ -226,17 +226,24 @@ func (s *Snapshot) diff(all []git.File) (change diff, err error) {
lastSeenModified, seen := lastModifiedTimes[f.Relative]
if !seen || modified.After(lastSeenModified) {
- change.put = append(change.put, f.Relative)
lastModifiedTimes[f.Relative] = modified
- // Keep track of remote names of local files so we can delete them later
+ // change separators to '/' for file paths in remote store
+ unixFileName := filepath.ToSlash(f.Relative)
+
+ // put file in databricks workspace
+ change.put = append(change.put, unixFileName)
+
+ // get file metadata about whether it's a notebook
isNotebook, typeOfNotebook, err := getNotebookDetails(f.Absolute)
if err != nil {
return change, err
}
- remoteName := f.Relative
+
+ // strip `.py` for python notebooks
+ remoteName := unixFileName
if isNotebook && typeOfNotebook == "PYTHON" {
- remoteName = strings.TrimSuffix(f.Relative, `.py`)
+ remoteName = strings.TrimSuffix(remoteName, `.py`)
}
// If the remote handle of a file changes, we want to delete the old
diff --git a/cmd/sync/snapshot_test.go b/cmd/sync/snapshot_test.go
index 54da750b..1fedb9fe 100644
--- a/cmd/sync/snapshot_test.go
+++ b/cmd/sync/snapshot_test.go
@@ -136,6 +136,40 @@ func TestDiff(t *testing.T) {
assert.Equal(t, map[string]string{"world.txt": "world.txt"}, state.RemoteToLocalNames)
}
+func TestFolderDiff(t *testing.T) {
+ // Create temp project dir
+ projectDir := t.TempDir()
+ fileSet := git.NewFileSet(projectDir)
+ state := Snapshot{
+ LastUpdatedTimes: make(map[string]time.Time),
+ LocalToRemoteNames: make(map[string]string),
+ RemoteToLocalNames: make(map[string]string),
+ }
+
+ err := os.Mkdir(filepath.Join(projectDir, "foo"), os.ModePerm)
+ assert.NoError(t, err)
+ f1 := createFile(t, filepath.Join(projectDir, "foo", "bar.py"))
+ defer f1.close(t)
+ f1.overwrite(t, "# Databricks notebook source\nprint(\"abc\")")
+
+ files, err := fileSet.All()
+ assert.NoError(t, err)
+ change, err := state.diff(files)
+ assert.NoError(t, err)
+ assert.Len(t, change.delete, 0)
+ assert.Len(t, change.put, 1)
+ assert.Contains(t, change.put, "foo/bar.py")
+
+ f1.remove(t)
+ files, err = fileSet.All()
+ assert.NoError(t, err)
+ change, err = state.diff(files)
+ assert.NoError(t, err)
+ assert.Len(t, change.delete, 1)
+ assert.Len(t, change.put, 0)
+ assert.Contains(t, change.delete, "foo/bar")
+}
+
func TestPythonNotebookDiff(t *testing.T) {
// Create temp project dir
projectDir := t.TempDir()