Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first tests for recursiveincludescanner

  • Loading branch information...
commit c041f06c456e33b214fe7879e1df7de7648e5cd7 1 parent 500f932
Lucas Meijer authored
View
4 CCompilerTask.cs
@@ -20,9 +20,9 @@ public CCompilerTask(string targetFile, string sourceFile, string[] includePaths
public IEnumerable<string> GetInputFiles(DependencyGraph depGraph)
{
- var scanner = new RecursiveIncludeScanner(_includePaths, new IncludeScanner());
+ var scanner = new RecursiveIncludeScanner(_includePaths, depGraph.IsTargetRegistered,new IncludeScanner().Scan);
yield return _sourceFile;
- foreach (var file in scanner.GetFilesIncludedBy(_sourceFile, depGraph))
+ foreach (var file in scanner.GetFilesIncludedBy(_sourceFile))
yield return file;
}
View
7 DependencyGraph.cs
@@ -5,6 +5,8 @@
namespace cake
{
+ public delegate bool WillFileBeGeneratedAtPath(string path);
+
public class DependencyGraph
{
public readonly Dictionary<string, TargetGenerateSettings> _graph = new Dictionary<string, TargetGenerateSettings>();
@@ -59,5 +61,10 @@ public void RegisterTarget(TargetGenerateSettings settings)
foreach(var targetFile in settings.OutputFiles)
_graph.Add(targetFile,settings);
}
+
+ public bool IsTargetRegistered(string file)
+ {
+ return _graph.Keys.Contains(file);
+ }
}
}
View
2  IncludeScanner.cs
@@ -6,6 +6,8 @@
namespace cake
{
+ public delegate IEnumerable<string> ScanFileForIncludes(string file);
+
public class IncludeScanner
{
static readonly Regex _regex = new Regex("^[ ]*#[ ]*include[ ]*[<\"]([^\">]*)[\">].*$", RegexOptions.Multiline);
View
14 MissingHeaderException.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace cake
+{
+ class MissingHeaderException : Exception
+ {
+ public MissingHeaderException(string file, string includedFile) : base("Unable to find header: "+includedFile+" which is being included by "+file)
+ {
+ }
+ }
+}
View
35 RecursiveIncludeScanner.cs
@@ -1,38 +1,55 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Linq;
namespace cake
{
public class RecursiveIncludeScanner
{
private readonly string[] _includePaths;
- private readonly IncludeScanner _includeScanner;
+ private readonly WillFileBeGeneratedAtPath _willFileBeGeneratedAtPath;
+ private readonly ScanFileForIncludes _scanFileForIncludes;
- public RecursiveIncludeScanner(string[] includePaths, IncludeScanner scanner)
+ public RecursiveIncludeScanner(IEnumerable<string> includePaths, WillFileBeGeneratedAtPath willFileBeGeneratedAtPath, ScanFileForIncludes scanFileForIncludes)
{
- _includePaths = includePaths;
- _includeScanner = scanner;
+ _includePaths = includePaths.ToArray();
+ _willFileBeGeneratedAtPath = willFileBeGeneratedAtPath;
+ _scanFileForIncludes = scanFileForIncludes;
}
public IEnumerable<string> GetFilesIncludedBy(string file)
{
- foreach (var includedFile in _includeScanner.Scan(file))
+ foreach (var includedFile in _scanFileForIncludes(file))
{
- var foundIncludeFile = FindSpecifiedIncludeFileInSearchDirs(includedFile);
+ var foundIncludeFile = FindSpecifiedIncludeFileInSearchDirs(file,includedFile);
if (foundIncludeFile == null)
- throw new InvalidOperationException("unable to find the header file: " + includedFile + " in the header search paths.");
+ throw new MissingHeaderException(file, includedFile);
yield return foundIncludeFile;
+
+ //this can happen if we found the right includefile in the depgraph, but it hasn't been generated yet.
if (!File.Exists(foundIncludeFile))
continue;
+
foreach (var file2 in GetFilesIncludedBy(foundIncludeFile))
yield return file2;
}
}
- private string FindSpecifiedIncludeFileInSearchDirs(string includedFile)
+ private string FindSpecifiedIncludeFileInSearchDirs(string includingFile, string includedFile)
{
- return includedFile;
+ var includeDirs = new List<string>() {Path.GetDirectoryName(includingFile)};
+ includeDirs.AddRange(_includePaths);
+
+ foreach(var dir in includeDirs)
+ {
+ var file_in_dir = Path.Combine(dir,Path.GetFileName(includedFile));
+ if (File.Exists(file_in_dir))
+ return file_in_dir;
+ if (_willFileBeGeneratedAtPath(file_in_dir))
+ return file_in_dir;
+ }
+ return null;
}
}
}
View
3  Tests/BenchMark.cs
@@ -3,8 +3,6 @@
using System.IO;
using System.Linq;
using System.Text;
-using cake;
-using cake.Tests;
using NUnit.Framework;
namespace cake.Tests
@@ -13,6 +11,7 @@ namespace cake.Tests
class BenchMark
{
[Test]
+ [Ignore]
public void Test()
{
var depGraph = new DependencyGraph();
View
7 Tests/DependencyGraphTests.cs
@@ -10,12 +10,7 @@ public class DependencyGraphTests
[SetUp]
public void Setup()
{
- const string dirName = "Workspace";
- if (Directory.Exists(dirName))
- Directory.Delete(dirName,true);
-
- Directory.CreateDirectory(dirName);
- Directory.SetCurrentDirectory(dirName);
+ Tools.SetupCleanCurrentDirectory();
_buildHistory = new BuildHistory();
_depGraph = new DependencyGraph(_buildHistory);
View
45 Tests/RecursiveIncludeScannerTest.cs
@@ -0,0 +1,45 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using Moq;
+using NUnit.Framework;
+
+namespace cake.Tests
+{
+ [TestFixture]
+ class RecursiveIncludeScannerTest
+ {
+ [SetUp]
+ public void Setup()
+ {
+ Tools.SetupCleanCurrentDirectory();
+ }
+
+ [Test]
+ public void CanFindHeaderInSameDirectory()
+ {
+ var ris = new RecursiveIncludeScanner(new string[0], file=>false,ScanFileMock);
+ File.WriteAllText("myheader.h", "//boss");
+ var result = ris.GetFilesIncludedBy("test.c");
+ Assert.AreEqual("myheader.h",result.Single());
+ }
+
+ [Test]
+ public void FailToFindNonExistingHeader()
+ {
+ WillFileBeGeneratedAtPath willFileBeGeneratedAtPath = file => false;
+ var ris = new RecursiveIncludeScanner(new string[0], willFileBeGeneratedAtPath, ScanFileMock);
+
+ Assert.Throws<MissingHeaderException>(()=>ris.GetFilesIncludedBy("test.c").ToArray());
+ }
+
+ private static IEnumerable<string> ScanFileMock(string file)
+ {
+ if (file != "test.c")
+ yield break;
+ yield return "myheader.h";
+ }
+ }
+}
View
21 Tests/Tools.cs
@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+
+namespace cake.Tests
+{
+ class Tools
+ {
+ public static void SetupCleanCurrentDirectory()
+ {
+ const string dirName = "Workspace";
+ if (Directory.Exists(dirName))
+ Directory.Delete(dirName,true);
+
+ Directory.CreateDirectory(dirName);
+ Directory.SetCurrentDirectory(dirName);
+ }
+ }
+}
View
3  cake.csproj
@@ -54,6 +54,7 @@
<Compile Include="BuildHistory.cs" />
<Compile Include="FileAssert.cs" />
<Compile Include="GenerationRecord.cs" />
+ <Compile Include="MissingHeaderException.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RecursiveIncludeScanner.cs" />
@@ -73,11 +74,13 @@
<Compile Include="IncludeScanner.cs" />
<Compile Include="Tests\GeneratedHeader.cs" />
<Compile Include="Tests\IncludeScannerTest.cs" />
+ <Compile Include="Tests\RecursiveIncludeScannerTest.cs" />
<Compile Include="Tests\SchedulableActionCollectorTest.cs" />
<Compile Include="Tests\SimpleAction.cs" />
<Compile Include="Tests\SimpleCopyDepGraph.cs" />
<Compile Include="ITargetGeneratingAction.cs" />
<Compile Include="Tests\TargetWithoutSources.cs" />
+ <Compile Include="Tests\Tools.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Please sign in to comment.
Something went wrong with that request. Please try again.