예제 #1
0
        // Given an msapp (original source of truth), stress test the conversions
        public static bool StressTest(string pathToMsApp)
        {
            try
            {
                using (var temp1 = new TempFile())
                {
                    string outFile = temp1.FullPath;

                    var log = TextWriter.Null;

                    // MsApp --> Model
                    CanvasDocument msapp;
                    ErrorContainer errors = new ErrorContainer();
                    try
                    {
                        msapp = MsAppSerializer.Load(pathToMsApp, errors); // read
                        errors.ThrowOnErrors();
                    }
                    catch (NotSupportedException)
                    {
                        errors.FormatNotSupported($"Too old: {pathToMsApp}");
                        return(false);
                    }

                    // Model --> MsApp
                    errors = msapp.SaveToMsApp(outFile);
                    errors.ThrowOnErrors();
                    var ok = MsAppTest.Compare(pathToMsApp, outFile, log);
                    if (!ok)
                    {
                        return(false);
                    }


                    // Model --> Source
                    using (var tempDir = new TempDir())
                    {
                        string outSrcDir = tempDir.Dir;
                        errors = msapp.SaveToSources(outSrcDir);
                        errors.ThrowOnErrors();

                        // Source --> Model
                        (var msapp2, var errors2) = CanvasDocument.LoadFromSources(outSrcDir);
                        errors2.ThrowOnErrors();

                        errors2 = msapp2.SaveToMsApp(outFile); // Write out .pa files.
                        errors2.ThrowOnErrors();
                        var ok2 = MsAppTest.Compare(pathToMsApp, outFile, log);
                        return(ok2);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(false);
            }
        }
        /// <summary>
        /// Save the document in a textual source format that can be checked into source control
        /// </summary>
        /// <param name="pathToSourceDirectory"></param>
        /// <param name="verifyOriginalPath">true if we should immediately repack the sources to verify they successfully roundtrip. </param>
        /// <returns></returns>
        public ErrorContainer SaveToSources(string pathToSourceDirectory, string verifyOriginalPath = null)
        {
            Utilities.EnsurePathRooted(pathToSourceDirectory);

            var errors = new ErrorContainer();

            Wrapper(() => SourceSerializer.SaveAsSource(this, pathToSourceDirectory, errors), errors);


            // Test that we can repack
            if (!errors.HasErrors && verifyOriginalPath != null)
            {
                (CanvasDocument msApp2, ErrorContainer errors2) = CanvasDocument.LoadFromSources(pathToSourceDirectory);
                if (errors2.HasErrors)
                {
                    errors2.PostUnpackValidationFailed();
                    return(errors2);
                }

                using (var temp = new TempFile())
                {
                    errors2 = msApp2.SaveToMsAppValidation(temp.FullPath);
                    if (errors2.HasErrors)
                    {
                        errors2.PostUnpackValidationFailed();
                        return(errors2);
                    }

                    bool ok = MsAppTest.Compare(verifyOriginalPath, temp.FullPath, TextWriter.Null);
                    if (!ok)
                    {
                        errors2.PostUnpackValidationFailed();
                        return(errors2);
                    }
                }
            }

            return(errors);
        }
        // Unpack, delete the entropy dirs, repack.
        public static CanvasDocument RemoveEntropy(string pathToMsApp)
        {
            using (var temp1 = new TempDir())
            {
                (CanvasDocument doc1, var errors) = CanvasDocument.LoadFromMsapp(pathToMsApp);
                errors.ThrowOnErrors();

                doc1.SaveToSources(temp1.Dir);

                var entropyDir = Path.Combine(temp1.Dir, "Entropy");
                if (!Directory.Exists(entropyDir))
                {
                    throw new Exception($"Missing entropy dir: " + entropyDir);
                }

                Directory.Delete(entropyDir, recursive: true);

                (var doc2, var errors2) = CanvasDocument.LoadFromSources(temp1.Dir);
                errors.ThrowOnErrors();

                return(doc2);
            }
        }