-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpackage-checks.cake
98 lines (80 loc) · 2.55 KB
/
package-checks.cake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// ***********************************************************************
// Copyright (c) Charlie Poole and contributors.
// Licensed under the MIT License. See LICENSE.txt in root directory.
// ***********************************************************************
//////////////////////////////////////////////////////////////////////
// SYNTAX FOR EXPRESSING CHECKS
//////////////////////////////////////////////////////////////////////
private static class Check
{
public static void That(string testDir, params PackageCheck[] checks)
{
foreach (var check in checks)
check.ApplyTo(testDir);
}
}
private static FileCheck HasFile(string file) => HasFiles(new[] { file });
private static FileCheck HasFiles(params string[] files) => new FileCheck(files);
private static DirectoryCheck HasDirectory(string dir) => new DirectoryCheck(dir);
//////////////////////////////////////////////////////////////////////
// PACKAGECHECK CLASS
//////////////////////////////////////////////////////////////////////
public abstract class PackageCheck
{
public abstract void ApplyTo(string testDir);
}
//////////////////////////////////////////////////////////////////////
// FILECHECK CLASS
//////////////////////////////////////////////////////////////////////
public class FileCheck : PackageCheck
{
string[] _files;
public FileCheck(string[] files)
{
_files = files;
}
public override void ApplyTo(string testDir)
{
foreach (string file in _files)
{
if (!System.IO.File.Exists(System.IO.Path.Combine(testDir, file)))
throw new Exception($"File {file} was not found.");
}
}
}
//////////////////////////////////////////////////////////////////////
// DIRECTORYCHECK CLASS
//////////////////////////////////////////////////////////////////////
public class DirectoryCheck : PackageCheck
{
private string _path;
private List<string> _files = new List<string>();
public DirectoryCheck(string path)
{
_path = path;
}
public DirectoryCheck WithFiles(params string[] files)
{
_files.AddRange(files);
return this;
}
public DirectoryCheck WithFile(string file)
{
_files.Add(file);
return this;
}
public override void ApplyTo(string testDir)
{
string combinedPath = System.IO.Path.Combine(testDir, _path);
if (!System.IO.Directory.Exists(combinedPath))
throw new Exception($"Directory {_path} was not found.");
if (_files != null)
{
foreach (var file in _files)
{
if (!System.IO.File.Exists(System.IO.Path.Combine(combinedPath, file)))
throw new Exception($"File {file} was not found in directory {_path}.");
}
}
}
}