Skip to content

Commit

Permalink
Properly tidy up temporary runsettings files
Browse files Browse the repository at this point in the history
 see #202
  • Loading branch information
SteveGilham committed Mar 12, 2024
1 parent 52d2e63 commit 9c87e56
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
8 changes: 7 additions & 1 deletion AltCover.Engine/Tasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ type Echo() =

true

[<SuppressMessage("Gendarme.Rules.Serialization",
"RelaxedMarkAllNonSerializableFieldsRule",
Justification = "Not going to do that")>]
type RunSettings() =
inherit Task(null)

Expand All @@ -354,6 +357,9 @@ type RunSettings() =
Justification = "Unit test accessor")>]
member val internal MessageIO: (string -> unit) option = None with get, set

member val internal GetTempFileName: Func<string> =
Func<string>(Path.GetTempFileName) with get, set

override self.Execute() =
let signal =
Option.defaultValue self.Message self.MessageIO
Expand All @@ -367,7 +373,7 @@ type RunSettings() =
|> sprintf "Settings Before: %s"
|> signal

let tempFile = Path.GetTempFileName()
let tempFile = self.GetTempFileName.Invoke()

try
let settings =
Expand Down
45 changes: 40 additions & 5 deletions AltCover.Tests/Tests3.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4455,10 +4455,29 @@ module AltCoverTests3 =
Console.SetOut(fst saved)
Console.SetError(snd saved)

let MakeRunSettings () =
let subject = RunSettings()

let temper =
subject
.GetType()
.GetProperty("GetTempFileName", BindingFlags.Instance ||| BindingFlags.NonPublic)

let basic =
temper.GetValue(subject) :?> Func<string>

let badge =
(fun () ->
let t1 = basic.Invoke()
Path.Combine(Path.GetDirectoryName t1, "altcover.test." + Path.GetFileName(t1)))

temper.SetValue(subject, Func<string>(badge))
subject

[<Test>]
let RunSettingsFailsIfCollectorNotFound () =
Main.init ()
let subject = RunSettings()
let subject = MakeRunSettings()

let dc =
subject
Expand All @@ -4474,6 +4493,7 @@ module AltCoverTests3 =
.GetProperty("MessageIO", BindingFlags.Instance ||| BindingFlags.NonPublic)

write.SetValue(subject, Some(fun (s: string) -> ()))

Assert.That(subject.Execute(), Is.False)
Assert.That(subject.Extended, Is.Empty)
CommandLine.verbosity <- 0
Expand All @@ -4495,7 +4515,7 @@ module AltCoverTests3 =
[<Test>]
let RunSettingsWorksIfOK () =
Main.init ()
let subject = RunSettings()
let subject = MakeRunSettings()

let dc =
subject
Expand All @@ -4517,9 +4537,18 @@ module AltCoverTests3 =
Assert.That(subject.Execute(), Is.True)
Assert.That(subject.Extended.EndsWith(".altcover.runsettings"))

Assert.That(
Path
.GetFileName(subject.Extended)
.StartsWith("altcover.test.")
)

let result =
subject.Extended |> File.ReadAllText

[ subject.Extended ]
|> Seq.iter (fun f -> maybeIOException (fun () -> File.Delete f))

Assert.That(
result
.Replace("\r", String.Empty)
Expand All @@ -4542,7 +4571,7 @@ module AltCoverTests3 =
[<Test>]
let RunSettingsExtendsOK () =
Main.init ()
let subject = RunSettings()
let subject = MakeRunSettings()

let dc =
subject
Expand Down Expand Up @@ -4571,6 +4600,9 @@ module AltCoverTests3 =
let result =
subject.Extended |> File.ReadAllText

[ subject.Extended ]
|> Seq.iter (fun f -> maybeIOException (fun () -> File.Delete f))

Assert.That(
result
.Replace("\r", String.Empty)
Expand All @@ -4593,7 +4625,7 @@ module AltCoverTests3 =
[<Test>]
let RunSettingsThrowsIfUninitialized () =
Main.init ()
let subject = RunSettings()
let subject = MakeRunSettings()

let dc =
subject
Expand All @@ -4616,7 +4648,7 @@ module AltCoverTests3 =
[<Test>]
let RunSettingsRecoversOK () =
Main.init ()
let subject = RunSettings()
let subject = MakeRunSettings()

let dc =
subject
Expand All @@ -4641,6 +4673,9 @@ module AltCoverTests3 =
let result =
subject.Extended |> File.ReadAllText

[ subject.Extended ]
|> Seq.iter (fun f -> maybeIOException (fun () -> File.Delete f))

Assert.That(
result
.Replace("\r", String.Empty)
Expand Down
31 changes: 22 additions & 9 deletions nupkg/build/AltCover.targets
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,27 @@
VSTestNoLogo="$(VSTestNoLogo)" />

<OnError ExecuteTargets="AltCoverGenerateCoverageResult" Condition="'$(AltCoverFailFast)' != 'true'" />
<OnError ExecuteTargets="AltCoverTidy" Condition="'$(AltCoverFailFast)' == 'true'" />
</Target>

<Target Name="AltCoverDeleteTempRunSettingsFile">
<Message Text="Deleting $(VSTestSetting)" />
<Delete Files="$(VSTestSetting)" >
<Output TaskParameter="DeletedFiles" ItemName="AltCoverDeletedList"/>
</Delete>
<Message Text="Deleted files: '@(AltCoverDeletedList)'"/>
</Target>

<Target Name="AltCoverTidy">
<ItemGroup>
<AltCoverRecorderGFiles Include="$(TargetDir)/AltCover.Recorder.g.*" />
<AltCoverSavedFiles Include="$(AltCoverSavedDirectory))_$(ProjectName)/*.*" />
</ItemGroup>
<CallTarget Targets="AltCoverDeleteTempRunSettingsFile"
Condition="$(VSTestSetting.EndsWith('.altcover.runsettings')) AND Exists($(VSTestSetting))"/>
<Move SourceFiles="@(AltCoverSavedFiles)" DestinationFolder="$(TargetDir)" OverwriteReadOnlyFiles="true" Condition="'$(AltCoverInPlace)' != 'false'" />
<RemoveDir Directories="$(AltCoverSavedDirectory)_$(ProjectName)" Condition="Exists('$(AltCoverSavedDirectory))_$(ProjectName)') AND '$(AltCoverInPlace)' != 'false'" />
<AltCover.RetryDelete Files="@(AltCoverRecorderGFiles)" Condition="'$(AltCoverInPlace)' != 'false'" />
</Target>

<Target Name="AltCoverGenerateCoverageResult" AfterTargets="VSTest" Condition="'$(AltCover)' == 'true'">
Expand All @@ -212,14 +233,6 @@
<AltCover.Echo Condition="'$(AltCoverShowSummary)' != '' AND '$(AltCoverSummary)' !=''"
Text="$(AltCoverSummary)" Colour="$(AltCoverColour)"
Verbosity="$(AltCoverVerbosity)" />
<ItemGroup>
<AltCoverRecorderGFiles Include="$(TargetDir)/AltCover.Recorder.g.*" />
<AltCoverSavedFiles Include="$(AltCoverSavedDirectory))_$(ProjectName)/*.*" />
<AltCoverRunSettings Include="$(TEMP)/*.altcover.runsettings" />
</ItemGroup>
<Delete Files="@(AltCoverRunSettings)" />
<Move SourceFiles="@(AltCoverSavedFiles)" DestinationFolder="$(TargetDir)" OverwriteReadOnlyFiles="true" Condition="'$(AltCoverInPlace)' != 'false'" />
<RemoveDir Directories="$(AltCoverSavedDirectory)_$(ProjectName)" Condition="Exists('$(AltCoverSavedDirectory))_$(ProjectName)') AND '$(AltCoverInPlace)' != 'false'" />
<AltCover.RetryDelete Files="@(AltCoverRecorderGFiles)" Condition="'$(AltCoverInPlace)' != 'false'" />
<CallTarget Targets="AltCoverTidy" />
</Target>
</Project>

0 comments on commit 9c87e56

Please sign in to comment.