private static void ValidateExistingChangeForEdit(ItemChange existingChange)
 {
     if ((existingChange.ChangeType & ItemChangeType.Delete) != 0)
     {
         throw new ApplicationException("Cannot edit a deleted item");
     }
 }
        public void DownloadCommitWithAddedBinaryItem()
        {
            var push = CreatePushRequest("b6f447775f71a092854a2555eea084bd6d19958e");
            var fakeDownloadRequest = new Fakes.StubIDownloadRequest()
            {
                DownloadChangesInCommitAsyncStringGuidCancellationToken = (commitId, repositoryId, token) =>
                {
                    var changes = new ItemChange[]
                        {
                            new ItemChange(ItemChangeType.Add, new Item("/favicon.ico"))
                        };
                    return Task.FromResult(changes.AsEnumerable());
                },
                DownloadItemAndContentInCommitAsyncItemChangeStringGuidCancellationToken = (itemChange, commitId, repositoryId, token) =>
                {
                    itemChange.Item.ContentMetadata = CreateBinaryContentMetadataTestData();
                    itemChange.NewContent = CreateBinaryContentTestData();
                    return Task.FromResult(0);
                }
            };
            var repo = new SourceRepository(fakeDownloadRequest);

            repo.DownloadChangesAsync(push, "/", CancellationToken.None).Wait();

            var change = push.Commits.Single().Changes.Single();
            Assert.AreEqual(ItemChangeType.Add, change.ChangeType);
            Assert.AreEqual("/favicon.ico", change.Item.Path);
            Assert.IsTrue(change.Item.ContentMetadata.IsBinary);
            Assert.AreEqual("image/x-icon", change.Item.ContentMetadata.ContentType);
            Assert.IsNotNull(change.NewContent);
            Assert.AreEqual(ItemContentType.Base64Encoded, change.NewContent.ContentType);
            Assert.AreEqual(12, change.NewContent.Content.Length);
        }
        public void PushItemChange()
        {
            var item = new Item("/test/test.txt")
            {
                ContentMetadata = new FileContentMetadata("text/plain", Encoding.UTF8)
            };
            var itemChange = new ItemChange(ItemChangeType.Add, item)
            {
                NewContent = new ItemContent(ItemContentType.RawText, "Testing")
            };
            var itemChanges = new ItemChange[] { itemChange };
            IEnumerable<ItemChange> itemChangesAdded = null;
            bool saveChangesCalled = false;
            var fakeContext = new Fakes.StubIDestinationContext()
            {
                AddItemChangesIEnumerableOfItemChange = (changes) => { itemChangesAdded = changes; },
                SaveChangesAsyncCancellationToken = (token) =>
                {
                    saveChangesCalled = true;
                    return Task.FromResult(0);
                }
            };
            var repo = new DestinationRepository(fakeContext);

            repo.PushItemChangesAsync(itemChanges, "/").Wait();

            Assert.IsNotNull(itemChangesAdded);
            Assert.AreEqual("test/test.txt", itemChangesAdded.Single().Item.Path);
            Assert.AreSame(itemChange.Item.ContentMetadata, itemChangesAdded.Single().Item.ContentMetadata);
            Assert.AreSame(itemChange.NewContent, itemChangesAdded.Single().NewContent);
            Assert.IsTrue(saveChangesCalled);
        }
 private static void ValidateExistingChangeForAdd(ItemChange existingItem)
 {
     if ((existingItem.ChangeType & ItemChangeType.Delete) == 0)
     {
         throw new ApplicationException("Cannot add item on an existing item");
     }
 }
        private static ItemChange EditExistingChange(ItemChange newChange, ItemChange existingChange)
        {
            ValidateExistingChangeForEdit(existingChange);

            var editedExistingItem = new ItemChange(existingChange.ChangeType, newChange.Item)
            {
                NewContent = newChange.NewContent
            };
            return editedExistingItem;
        }
 private void AddChange(ItemChange change)
 {
     if (_items.ContainsKey(change.Item.Path))
     {
         ValidateExistingChangeForAdd(_items[change.Item.Path]);
         _items[change.Item.Path] = change;
     }
     else
     {
         _items.Add(change.Item.Path, change);
     }
 }
        public void GetBinaryItemAndBlob()
        {
            var repo = CreateDownloadRequest();
            var change = new ItemChange(ItemChangeType.Add, new Item("/favicon.ico"));

            repo.DownloadItemAndContentInCommitAsync(
                change,
                "b6f447775f71a092854a2555eea084bd6d19958e",
                Guid.Parse("0ad49569-db8b-4a8a-b5cc-f7ff009949c8"),
                CancellationToken.None
                ).Wait();

            Assert.IsNotNull(change.Item.ContentMetadata);
            Assert.AreEqual("image/x-icon", change.Item.ContentMetadata.ContentType);
            Assert.IsTrue(change.Item.ContentMetadata.IsBinary);
            Assert.IsNotNull(change.NewContent);
            Assert.AreEqual(ItemContentType.Base64Encoded, change.NewContent.ContentType);
            Assert.AreEqual(1536, change.NewContent.Content.Length);
        }
Ejemplo n.º 8
0
        public async Task DownloadItemAndContentInCommitAsync(ItemChange change, string commitId, Guid repositoryId, CancellationToken token)
        {
            CreateHttpClient();

            var item = await _httpClient.GetItemAsync(
                repositoryId,
                change.Item.Path,
                includeContentMetadata: true,
                versionDescriptor: new Microsoft.TeamFoundation.SourceControl.WebApi.GitVersionDescriptor()
                {
                    VersionType = Microsoft.TeamFoundation.SourceControl.WebApi.GitVersionType.Commit,
                    Version = commitId
                },
                cancellationToken: token
                );

            change.Item.ContentMetadata = item.ContentMetadata.ToSync();

            if (!item.IsFolder)
            {
                var content = await _httpClient.GetBlobContentAsync(repositoryId, item.ObjectId, cancellationToken: token);
                await change.SetNewContentAsync(content, token);
            }
        }
 private static bool IsItemAdded(ItemChange existingChange)
 {
     return (existingChange.ChangeType & (ItemChangeType.Add | ItemChangeType.Rename)) != 0;
 }
        public void DownloadNoChangesInRoot()
        {
            var push = CreatePushRequest("1b1859c414e800d24036b9ee547d1530431ae055");
            var fakeDownloadRequest = new Fakes.StubIDownloadRequest()
            {
                DownloadChangesInCommitAsyncStringGuidCancellationToken = (commit, repositoryId, token) =>
                {
                    var changes = new ItemChange[]
                        {
                            new ItemChange(ItemChangeType.Edit, new Item("/index.html")),
                            new ItemChange(ItemChangeType.Add, new Item("/index2.html"))
                        };
                    return Task.FromResult(changes.AsEnumerable());
                }
            };
            var repo = new SourceRepository(fakeDownloadRequest);

            repo.DownloadChangesAsync(push, "/fake/", CancellationToken.None).Wait();

            Assert.AreEqual(0, push.Commits.Single().Changes.Count());
        }
Ejemplo n.º 11
0
 private void RenameChange(ItemChange change)
 {
     AddChange(change);
 }
        public void DownloadCommitWithRenamedTextItem()
        {
            var push = CreatePushRequest("de3e7a550c40fe75085d11e81d5770bc5b0dd33c");
            var fakeDownloadRequest = new Fakes.StubIDownloadRequest()
            {
                DownloadChangesInCommitAsyncStringGuidCancellationToken = (commitId, repositoryId, token) =>
                {
                    var changes = new ItemChange[]
                        {
                            new ItemChange(ItemChangeType.Rename, new Item("/index.html")),
                            new ItemChange(ItemChangeType.Delete | ItemChangeType.SourceRename, new Item("/index2.html"))
                        };
                    return Task.FromResult(changes.AsEnumerable());
                },
                DownloadItemAndContentInCommitAsyncItemChangeStringGuidCancellationToken = (itemChange, commitId, repositoryId, token) =>
                {
                    if (itemChange.Item.Path == "/index.html")
                    {
                        itemChange.Item.ContentMetadata = CreateTextContentMetadataTestData();
                        itemChange.NewContent = CreateTextContentTestData();
                    }
                    return Task.FromResult(0);
                }
            };
            var repo = new SourceRepository(fakeDownloadRequest);

            repo.DownloadChangesAsync(push, "/", CancellationToken.None).Wait();

            var index2Item = push.Commits.Single().Changes.Single(c => c.Item.Path == "/index2.html");
            Assert.AreEqual(ItemChangeType.Delete | ItemChangeType.SourceRename, index2Item.ChangeType);

            var indexItem = push.Commits.Single().Changes.Single(c => c.Item.Path == "/index.html");
            Assert.AreEqual(ItemChangeType.Rename, indexItem.ChangeType);
            Assert.IsNotNull(indexItem.Item.ContentMetadata);
            Assert.IsFalse(indexItem.Item.ContentMetadata.IsBinary);
            Assert.AreEqual("text/html", indexItem.Item.ContentMetadata.ContentType);
            Assert.IsNotNull(indexItem.NewContent);
            Assert.AreEqual(ItemContentType.RawText, indexItem.NewContent.ContentType);
            Assert.AreEqual("Testing", indexItem.NewContent.Content);
        }
        public void GetTextItemAndBlob()
        {
            var repo = CreateDownloadRequest();
            var change = new ItemChange(ItemChangeType.Add, new Item("/index.html"));

            repo.DownloadItemAndContentInCommitAsync(
                change,
                "5597f65ce55386a771e4bf6fa190b5a26c0f5ce5",
                Guid.Parse("0ad49569-db8b-4a8a-b5cc-f7ff009949c8"),
                CancellationToken.None
                ).Wait();

            Assert.IsNotNull(change.Item.ContentMetadata);
            Assert.AreEqual("text/html", change.Item.ContentMetadata.ContentType);
            Assert.AreEqual(65001, change.Item.ContentMetadata.Encoding.CodePage);
            Assert.IsFalse(change.Item.ContentMetadata.IsBinary);
            Assert.IsNotNull(change.NewContent);
            Assert.AreEqual(ItemContentType.RawText, change.NewContent.ContentType);
            Assert.AreEqual(119, change.NewContent.Content.Length);
        }
Ejemplo n.º 14
0
 private void DeleteChange(ItemChange change)
 {
     if (_items.ContainsKey(change.Item.Path))
     {
         if (IsItemAdded(_items[change.Item.Path]))
         {
             _items.Remove(change.Item.Path);
         }
         else
         {
             _items[change.Item.Path] = change;
         }
     }
     else
     {
         _items.Add(change.Item.Path, change);
     }
 }
        public void DownloadCommitWithAddedTextItem()
        {
            var push = CreatePushRequest("5597f65ce55386a771e4bf6fa190b5a26c0f5ce5");
            var fakeDownloadRequest = new Fakes.StubIDownloadRequest()
            {
                DownloadChangesInCommitAsyncStringGuidCancellationToken = (commitId, repositoryId, token) =>
                {
                    var changes = new ItemChange[]
                        {
                            new ItemChange(ItemChangeType.Add, new Item("/index.html"))
                        };
                    return Task.FromResult(changes.AsEnumerable());
                },
                DownloadItemAndContentInCommitAsyncItemChangeStringGuidCancellationToken = (itemChange, commitId, repositoryId, token) =>
                {
                    itemChange.Item.ContentMetadata = CreateTextContentMetadataTestData();
                    itemChange.NewContent = CreateTextContentTestData();
                    return Task.FromResult(0);
                }
            };
            var repo = new SourceRepository(fakeDownloadRequest);

            repo.DownloadChangesAsync(push, "/", CancellationToken.None).Wait();

            var change = push.Commits.Single().Changes.Single();
            Assert.AreEqual(ItemChangeType.Add, change.ChangeType);
            Assert.AreEqual("/index.html", change.Item.Path);
            Assert.IsNotNull(change.Item.ContentMetadata);
            Assert.IsFalse(change.Item.ContentMetadata.IsBinary);
            Assert.AreEqual("text/html", change.Item.ContentMetadata.ContentType);
            Assert.IsNotNull(change.NewContent);
            Assert.AreEqual(ItemContentType.RawText, change.NewContent.ContentType);
            Assert.AreEqual("Testing", change.NewContent.Content);
        }
 private IItemCommand CreateUploadCommand(ItemChange itemChange)
 {
     return new UploadItemCommand(itemChange);
 }
 private IItemCommand CreateDeleteCommand(ItemChange itemChange)
 {
     return new DeleteItemCommand(itemChange);
 }
Ejemplo n.º 18
0
 public UploadItemCommand(ItemChange itemChange)
 {
     _itemChange = itemChange;
 }
Ejemplo n.º 19
0
 public DeleteItemCommand(ItemChange itemChange)
 {
     _itemChange = itemChange;
 }
        public void GetSubfolder()
        {
            var repo = CreateDownloadRequest();
            var change = new ItemChange(ItemChangeType.Add, new Item("/subfolder"));

            repo.DownloadItemAndContentInCommitAsync(
                change,
                "1f8734c1b02957390f27e79638b2bd96374b00a2",
                Guid.Parse("0ad49569-db8b-4a8a-b5cc-f7ff009949c8"),
                CancellationToken.None
                ).Wait();

            Assert.IsNotNull(change.Item.ContentMetadata);
            Assert.IsNull(change.NewContent);
        }
Ejemplo n.º 21
0
 private void EditChange(ItemChange change)
 {
     if (_items.ContainsKey(change.Item.Path))
     {
         _items[change.Item.Path] = EditExistingChange(change, _items[change.Item.Path]);
     }
     else
     {
         _items.Add(change.Item.Path, change);
     }
 }
        public void GetTextItemAndBlobWhenCanceled()
        {
            var repo = CreateDownloadRequest();
            var change = new ItemChange(ItemChangeType.Add, new Item("/index.html"));

            var task = repo.DownloadItemAndContentInCommitAsync(
                change,
                "5597f65ce55386a771e4bf6fa190b5a26c0f5ce5",
                Guid.Parse("0ad49569-db8b-4a8a-b5cc-f7ff009949c8"),
                new CancellationToken(true));

            try
            {
                task.Wait();
            }
            catch (AggregateException e)
            {
                throw e.InnerException;
            }
        }
Ejemplo n.º 23
0
        private void ProcessChange(ItemChange change)
        {
            Debug.WriteLine("{0} {1}", change.ChangeType.ToString(), change.Item.Path);

            if ((change.ChangeType & ItemChangeType.Add) != 0)
            {
                AddChange(change);
            }
            else if ((change.ChangeType & ItemChangeType.Delete) != 0)
            {
                DeleteChange(change);
            }
            else if ((change.ChangeType & ItemChangeType.Edit) != 0)
            {
                EditChange(change);
            }
            else if ((change.ChangeType & ItemChangeType.Rename) != 0)
            {
                RenameChange(change);
            }
            else
            {
                // Ignore all other types
            }
        }
Ejemplo n.º 24
0
 private static bool ChangeTypeHasContent(ItemChange change)
 {
     return (change.ChangeType & (ItemChangeType.Edit | ItemChangeType.Add | ItemChangeType.Rename)) != 0;
 }
        public void DownloadCommitWithChanges()
        {
            var commitIdRequest = "1b1859c414e800d24036b9ee547d1530431ae055";
            var pushRequest = CreatePushRequest(commitIdRequest);
            var fakeChanges = new ItemChange[] { new ItemChange(ItemChangeType.None, new Item("/index.html")) };
            var fakeDownloadRequest = new Fakes.StubIDownloadRequest()
            {
                DownloadChangesInCommitAsyncStringGuidCancellationToken = (commitId, repositoryId, token) =>
                {
                    Assert.AreEqual(commitIdRequest, commitId);
                    return Task.FromResult(fakeChanges.AsEnumerable());
                }
            };
            var repo = new SourceRepository(fakeDownloadRequest);

            repo.DownloadChangesAsync(pushRequest, "/", CancellationToken.None).Wait();

            Assert.IsTrue(fakeChanges.SequenceEqual(pushRequest.Commits.Single().Changes));
        }