public async Task SaveAsNewExpectedAsync(bool updateOriginals, bool updateJson, bool updateImage)
        {
            try
            {
                if (!updateOriginals && !updateJson && !updateImage)
                {
                    return;
                }

                // If actual has error and existing did NOT have error, delete existing image
                if (TestResult.Error != null && ExpectedImageFile != null && updateImage)
                {
                    try
                    {
                        await ExpectedImageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
                    }
                    catch { }
                }

                // Save the updated info
                await new StoredTestResultInfo()
                {
                    HostConfigHash = updateOriginals ? HostConfigFile.Hash : _oldHostConfigHash,
                    CardHash       = updateOriginals ? CardFile.Hash : _oldCardHash,
                    Error          = (updateJson || updateImage) ? TestResult.Error : ExpectedError
                }.SaveToFileAsync(_expectedFolder, _expectedFileNameWithoutExtension);

                // Make sure the source files are saved (we use FailIfExists and try/catch since if file already exists no need to update it)
                if (updateOriginals)
                {
                    try
                    {
                        var sourceHostConfigFile = await _sourceHostConfigsFolder.CreateFileAsync(GetStoredSourceFileName(HostConfigFile.Name, HostConfigFile.Hash), CreationCollisionOption.OpenIfExists);

                        await FileIO.WriteTextAsync(sourceHostConfigFile, HostConfigFile.Contents);

                        var oldSourceHostConfigFile = await _sourceHostConfigsFolder.CreateFileAsync(GetStoredSourceFileName(HostConfigFile.Name, _oldHostConfigHash), CreationCollisionOption.OpenIfExists);

                        await oldSourceHostConfigFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
                    }
                    catch { }
                    try
                    {
                        var sourceCardFile = await _sourceCardsFolder.CreateFileAsync(GetStoredSourceFileName(CardFile.Name, CardFile.Hash), CreationCollisionOption.OpenIfExists);

                        await FileIO.WriteTextAsync(sourceCardFile, CardFile.Contents);

                        var oldSourceCardFile = await _sourceCardsFolder.CreateFileAsync(GetStoredSourceFileName(CardFile.Name, _oldCardHash), CreationCollisionOption.OpenIfExists);

                        await oldSourceCardFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
                    }
                    catch { }
                }

                // If no error, update the image file
                if (TestResult.Error == null)
                {
                    if (updateImage)
                    {
                        var expectedFile = await _expectedFolder.CreateFileAsync(_expectedFileNameWithoutExtension + ".png", CreationCollisionOption.ReplaceExisting);

                        await ActualImageFile.CopyAndReplaceAsync(expectedFile);
                    }
                    if (updateJson)
                    {
                        var expectedJsonFile = await _expectedFolder.CreateFileAsync(GetStrippedFileName(CardFile) + "ToJson.json", CreationCollisionOption.ReplaceExisting);

                        await ActualRoundTrippedJsonFile.CopyAndReplaceAsync(expectedJsonFile);
                    }
                }

                // Create a new status with the values set to the current values
                TestStatus newStatus = new TestStatus();
                newStatus.ImageMatched         = Status.ImageMatched;
                newStatus.JsonRoundTripMatched = Status.JsonRoundTripMatched;
                newStatus.MatchedViaError      = Status.MatchedViaError;
                newStatus.NewCard         = Status.NewCard;
                newStatus.OriginalMatched = Status.OriginalMatched;

                // Update based on the changes
                if ((TestResult.Error != null) && (updateImage || updateJson))
                {
                    newStatus.MatchedViaError = true;
                    ExpectedError             = TestResult.Error;
                }
                else
                {
                    if (updateImage)
                    {
                        newStatus.ImageMatched = true;
                        ExpectedImageFile      = ActualImageFile;
                    }
                    if (updateJson)
                    {
                        newStatus.JsonRoundTripMatched = true;
                    }
                }

                if (updateOriginals)
                {
                    _oldHostConfigHash        = HostConfigFile.Hash;
                    _oldCardHash              = CardFile.Hash;
                    newStatus.OriginalMatched = true;
                    newStatus.NewCard         = false;
                }

                Status = newStatus;
            }
            catch (Exception ex)
            {
                var dontWait = new MessageDialog(ex.ToString()).ShowAsync();
            }
        }
Esempio n. 2
0
        public async Task SaveAsNewExpectedAsync()
        {
            try
            {
                // If actual has error and existing did NOT have error, delete existing image
                if (ActualError != null && ExpectedImageFile != null)
                {
                    try
                    {
                        await ExpectedImageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
                    }
                    catch { }
                }

                // Save the updated info
                await new StoredTestResultInfo()
                {
                    HostConfigHash = HostConfigFile.Hash,
                    CardHash       = CardFile.Hash,
                    Error          = ActualError
                }.SaveToFileAsync(_expectedFolder, _expectedFileNameWithoutExtension);

                // Make sure the source files are saved (we use FailIfExists and try/catch since if file already exists no need to update it)
                try
                {
                    var sourceHostConfigFile = await _sourceHostConfigsFolder.CreateFileAsync(GetStoredSourceFileName(HostConfigFile.Name, HostConfigFile.Hash), CreationCollisionOption.FailIfExists);

                    await FileIO.WriteTextAsync(sourceHostConfigFile, HostConfigFile.Contents);

                    var oldSourceHostConfigFile = await _sourceHostConfigsFolder.CreateFileAsync(GetStoredSourceFileName(HostConfigFile.Name, _oldHostConfigHash), CreationCollisionOption.OpenIfExists);

                    await oldSourceHostConfigFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
                }
                catch { }
                try
                {
                    var sourceCardFile = await _sourceCardsFolder.CreateFileAsync(GetStoredSourceFileName(CardFile.Name, CardFile.Hash), CreationCollisionOption.FailIfExists);

                    await FileIO.WriteTextAsync(sourceCardFile, CardFile.Contents);

                    var oldSourceCardFile = await _sourceCardsFolder.CreateFileAsync(GetStoredSourceFileName(CardFile.Name, _oldCardHash), CreationCollisionOption.OpenIfExists);

                    await oldSourceCardFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
                }
                catch { }

                // If no error, update the image file
                if (ActualError == null)
                {
                    var expectedFile = await _expectedFolder.CreateFileAsync(_expectedFileNameWithoutExtension + ".png", CreationCollisionOption.ReplaceExisting);

                    await ActualImageFile.CopyAndReplaceAsync(expectedFile);

                    var expectedJsonFile = await _expectedFolder.CreateFileAsync(GetStrippedFileName(CardFile) + "ToJson.json", CreationCollisionOption.ReplaceExisting);

                    await ActualRoundTrippedJsonFile.CopyAndReplaceAsync(expectedJsonFile);
                }

                // Update the status
                ExpectedError      = ActualError;
                ExpectedImageFile  = ActualImageFile;
                _oldHostConfigHash = HostConfigFile.Hash;
                _oldCardHash       = CardFile.Hash;
                Status             = TestStatus.Passed;
            }
            catch (Exception ex)
            {
                var dontWait = new MessageDialog(ex.ToString()).ShowAsync();
            }
        }