diff --git a/AutoRest/AutoRest.Core.Tests/CodeGeneratorsTests.cs b/AutoRest/AutoRest.Core.Tests/CodeGeneratorsTests.cs index c58c81e4e6..8cadb3f641 100644 --- a/AutoRest/AutoRest.Core.Tests/CodeGeneratorsTests.cs +++ b/AutoRest/AutoRest.Core.Tests/CodeGeneratorsTests.cs @@ -86,8 +86,11 @@ public void OutputToSingleFile() }; string path = Path.Combine(settings.OutputDirectory, "test.file.cs"); + string existingContents = "this is dummy"; + _fileSystem.VirtualStore[path] = new StringBuilder(existingContents); var codeGenerator = new SampleCodeGenerator(settings); codeGenerator.Generate(new ServiceClient()).GetAwaiter().GetResult(); + Assert.DoesNotContain(existingContents, _fileSystem.VirtualStore[path].ToString()); Assert.Equal(4, _fileSystem.VirtualStore.Count); Assert.True(_fileSystem.VirtualStore.ContainsKey(path)); Assert.True(_fileSystem.VirtualStore.ContainsKey("AutoRest.json")); diff --git a/AutoRest/AutoRest.Core/CodeGenerator.cs b/AutoRest/AutoRest.Core/CodeGenerator.cs index 8efb1c0311..57dbecb697 100644 --- a/AutoRest/AutoRest.Core/CodeGenerator.cs +++ b/AutoRest/AutoRest.Core/CodeGenerator.cs @@ -16,6 +16,7 @@ namespace Microsoft.Rest.Generator public abstract class CodeGenerator { public const string EnumObject = "x-ms-enum"; + private bool firstTimeWriteSingleFile = true; protected CodeGenerator(Settings settings) { @@ -99,7 +100,7 @@ public async Task Write(ITemplate template, string fileName) /// public async Task Write(string template, string fileName) { - string relativeFilePath = null; + string filePath = null; if (Settings.OutputFileName != null) { @@ -110,17 +111,19 @@ public async Task Write(string template, string fileName) ErrorManager.ThrowErrors(); } - relativeFilePath = Settings.OutputFileName; + filePath = Path.Combine(Settings.OutputDirectory, Settings.OutputFileName); + + if (firstTimeWriteSingleFile) + { + // for SingleFileGeneration clean the file before writing only if its the first time + Settings.FileSystem.DeleteFile(filePath); + firstTimeWriteSingleFile = false; + } } else { - relativeFilePath = fileName; - } - string filePath = Path.Combine(Settings.OutputDirectory, relativeFilePath); - - // cleans file before writing unless single file - if (!(Settings.OutputFileName != null && IsSingleFileGenerationSupported)) - { + filePath = Path.Combine(Settings.OutputDirectory, fileName); + // cleans file before writing Settings.FileSystem.DeleteFile(filePath); } // Make sure the directory exist