Skip to content

Commit

Permalink
Add standard format header to new files
Browse files Browse the repository at this point in the history
Fixes #53
  • Loading branch information
kzu committed Apr 26, 2021
1 parent 0aa9785 commit 1a64af0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Config.Tests/ConfigDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,5 +697,22 @@ public void when_saving_variable_with_hash_then_adds_quotes()

Assert.Contains("\"", line);
}

[Fact]
public void when_saving_new_file_then_adds_standard_comment_line()
{
var temp = Path.GetTempFileName();
if (File.Exists(temp))
File.Delete(temp);

ConfigDocument
.FromFile(temp)
.Set("foo", null, "bar", "baz")
.Save();

var line = File.ReadAllLines(temp).First();

Assert.StartsWith("# .netconfig", line);
}
}
}
9 changes: 8 additions & 1 deletion src/Config/ConfigDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -45,7 +45,14 @@ public ConfigDocument Save()
if (!Directory.Exists(Path.GetDirectoryName(filePath)))
Directory.CreateDirectory(Path.GetDirectoryName(filePath));

var addHeader = !File.Exists(filePath);
using var writer = new StreamWriter(filePath, false);
if (addHeader)
{
writer.WriteLine("# .netconfig is awesome: https://dotnetconfig.org");
writer.WriteLine();
}

foreach (var line in Lines)
{
writer.WriteLine(line.LineText);
Expand Down

0 comments on commit 1a64af0

Please sign in to comment.