예제 #1
0
        public void 列舉根路徑內的子資料夾()
        {
            var rootUPath = CreateRootPath();

            using var fileSystem = new MemoryFileSystem();
            var subName = "../../path";

            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";

            var uPath = UPath.Combine(rootUPath, "..");

            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            var directoryEntries = fileSystem.EnumerateDirectoryEntries(subPath);

            foreach (var entry in directoryEntries)
            {
                Console.WriteLine(entry.Path);
            }

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));
        }
예제 #2
0
        public void 修改檔案日期()
        {
            var rootUPath = CreateRootPath();

            using var fileSystem = new MemoryFileSystem();

            var subName = "TestFolder";

            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subFile2     = $"{subPath}/1/2.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            var fileEntry = fileSystem.GetFileEntry(subFile1);

            fileEntry.CreationTime   = new DateTime(1900, 1, 1);
            fileEntry.LastWriteTime  = new DateTime(1900, 1, 2);
            fileEntry.LastAccessTime = new DateTime(1900, 1, 3);

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));

            fileSystem.DeleteDirectory(subPath, true);
        }
예제 #3
0
        public void 在資料夾建立檔案()
        {
            var rootUPath = CreateRootPath();

            using var fileSystem = new MemoryFileSystem();

            var subName      = "TestFolder";
            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));
        }
예제 #4
0
        public async Task SupportModelsNameOverride()
        {
            string modelsName = "MyModels";

            MemoryFileSystem fileSystem = CreateMockFilesystem();

            var settings = new Settings
            {
                Modeler         = "Swagger",
                CodeGenerator   = "CSharp",
                FileSystem      = fileSystem,
                OutputDirectory = "GeneratedCode",
                Namespace       = "Test",
                ModelsName      = modelsName
            };

            using (fileSystem = $"{GetType().Name}.yaml".GenerateCodeInto(fileSystem, settings))
            {
                // Expected Files
                Assert.True(fileSystem.FileExists($@"{settings.OutputDirectory}\{modelsName}\ResultObject.cs"));

                var result = await Compile(fileSystem);

                // filter the warnings
                var warnings = result.Messages.Where(
                    each => each.Severity == DiagnosticSeverity.Warning &&
                    !SuppressWarnings.Contains(each.Id)).ToArray();

                // use this to dump the files to disk for examination
                // fileSystem.SaveFilesToTemp($"{GetType().Name}");

                // filter the errors
                var errors = result.Messages.Where(each => each.Severity == DiagnosticSeverity.Error).ToArray();

                Write(warnings, fileSystem);
                Write(errors, fileSystem);

                // use this to write out all the messages, even hidden ones.
                // Write(result.Messages, fileSystem);

                // Don't proceed unless we have zero Warnings.
                Assert.Empty(warnings);

                // Don't proceed unless we have zero Errors.
                Assert.Empty(errors);

                // Should also succeed.
                Assert.True(result.Succeeded);

                // try to load the assembly
                var asm = Assembly.Load(result.Output.GetBuffer());
                Assert.NotNull(asm);

                // verify that we have the class we expected
                var resultObject = asm.ExportedTypes.FirstOrDefault(each => each.FullName == $"Test.{modelsName}.ResultObject");
                Assert.NotNull(resultObject);
            }
        }
예제 #5
0
        internal static MemoryFileSystem GenerateCodeInto(this string testName, MemoryFileSystem fileSystem, Settings settings)
        {
            // copy the whole input directory into the memoryfilesystem.
            fileSystem.CopyFolder("Resource", testName, "");

            // find the appropriately named .yaml or .json file for the swagger.
            foreach (var ext in new[] { ".yaml", ".json", ".md" })
            {
                var name = testName + ext;
                if (fileSystem.FileExists(name))
                {
                    settings.Input = name;
                }
            }

            if (string.IsNullOrWhiteSpace(settings.Input))
            {
                throw new Exception($"Can't find swagger file ${testName} [.yaml] [.json] [.md]");
            }

            var plugin    = ExtensionsLoader.GetPlugin();
            var modeler   = ExtensionsLoader.GetModeler();
            var codeModel = modeler.Build();

            // After swagger Parser
            codeModel = AutoRestController.RunExtensions(Trigger.AfterModelCreation, codeModel);

            // After swagger Parser
            codeModel = AutoRestController.RunExtensions(Trigger.BeforeLoadingLanguageSpecificModel, codeModel);

            using (plugin.Activate())
            {
                // load model into language-specific code model
                codeModel = plugin.Serializer.Load(codeModel);

                // we've loaded the model, run the extensions for after it's loaded
                codeModel = AutoRestController.RunExtensions(Trigger.AfterLoadingLanguageSpecificModel, codeModel);

                // apply language-specific tranformation (more than just language-specific types)
                // used to be called "NormalizeClientModel" .
                codeModel = plugin.Transformer.TransformCodeModel(codeModel);

                // next set of extensions
                codeModel = AutoRestController.RunExtensions(Trigger.AfterLanguageSpecificTransform, codeModel);

                // next set of extensions
                codeModel = AutoRestController.RunExtensions(Trigger.BeforeGeneratingCode, codeModel);

                // Generate code from CodeModel.
                plugin.CodeGenerator.Generate(codeModel).GetAwaiter().GetResult();
            }

            return(fileSystem);
        }
예제 #6
0
        public void ExampleUsage1()
        {
            IFileSystem fs           = new MemoryFileSystem();
            UPath       filePath1    = "/temp.txt";
            string      fileContent1 = "This is a content";

            fs.WriteAllText(filePath1, fileContent1);
            Assert.True(fs.FileExists(filePath1));
            Assert.Equal(fileContent1, fs.ReadAllText(filePath1));
            Assert.Single(fs.EnumerateFileSystemEntries(UPath.Root));
        }
예제 #7
0
        public void CopyAndMoveFileCross()
        {
            var mountfs = new MountFileSystem();
            var memfs1  = new MemoryFileSystem();

            memfs1.WriteAllText("/file1.txt", "content1");
            var memfs2 = new MemoryFileSystem();

            mountfs.Mount("/dir1", memfs1);
            mountfs.Mount("/dir2", memfs2);

            mountfs.CopyFile("/dir1/file1.txt", "/dir2/file2.txt", true);

            Assert.True(memfs2.FileExists("/file2.txt"));
            Assert.Equal("content1", memfs2.ReadAllText("/file2.txt"));

            mountfs.MoveFile("/dir1/file1.txt", "/dir2/file1.txt");

            Assert.False(memfs1.FileExists("/file1.txt"));
            Assert.True(memfs2.FileExists("/file1.txt"));
            Assert.Equal("content1", memfs2.ReadAllText("/file1.txt"));
        }
        internal static MemoryFileSystem GenerateCodeInto(this string testName, MemoryFileSystem inputFileSystem, Settings settings, IAnyPlugin plugin = null)
        {
            plugin = plugin ?? new PluginCs();
            Settings.Instance.FileSystemInput = inputFileSystem;
            // copy the whole input directory into the memoryfilesystem.
            inputFileSystem.CopyFolder("Resource", testName, "");

            // find the appropriately named .yaml or .json file for the swagger.
            foreach (var ext in new[] { ".yaml", ".json", ".md" })
            {
                var name = testName + ext;
                if (inputFileSystem.FileExists(name))
                {
                    settings.Input = name;
                }
            }

            if (string.IsNullOrWhiteSpace(settings.Input))
            {
                throw new Exception($"Can't find swagger file ${testName} [.yaml] [.json] [.md]");
            }

            var modeler   = new SwaggerModeler(Settings.Instance);
            var swagger   = SwaggerParser.Parse(inputFileSystem.ReadAllText(Settings.Instance.Input));
            var codeModel = modeler.Build(swagger);

            using (plugin.Activate())
            {
                // load model into language-specific code model
                codeModel = plugin.Serializer.Load(codeModel);

                // apply language-specific tranformation (more than just language-specific types)
                // used to be called "NormalizeClientModel" .
                codeModel = plugin.Transformer.TransformCodeModel(codeModel);

                // Generate code from CodeModel.
                plugin.CodeGenerator.Generate(codeModel).GetAwaiter().GetResult();

                return(settings.FileSystemOutput);
            }
        }