1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
+
4
+ using System ;
5
+ using System . IO ;
6
+ using System . Linq ;
7
+ using System . Text . RegularExpressions ;
8
+ using System . Threading . Tasks ;
9
+ using Newtonsoft . Json ;
10
+ using Newtonsoft . Json . Linq ;
11
+ using Templates . Test . Helpers ;
12
+ using Xunit ;
13
+ using Xunit . Abstractions ;
14
+
15
+ namespace Templates . Test
16
+ {
17
+ public class BaselineTest
18
+ {
19
+ private static readonly Regex TemplateNameRegex = new Regex (
20
+ "new (?<template>[a-zA-Z]+)" ,
21
+ RegexOptions . Compiled | RegexOptions . ExplicitCapture | RegexOptions . Singleline ,
22
+ TimeSpan . FromSeconds ( 1 ) ) ;
23
+
24
+ private static readonly Regex AuthenticationOptionRegex = new Regex (
25
+ "-au (?<auth>[a-zA-Z]+)" ,
26
+ RegexOptions . Compiled | RegexOptions . ExplicitCapture | RegexOptions . Singleline ,
27
+ TimeSpan . FromSeconds ( 1 ) ) ;
28
+
29
+ private static readonly Regex LanguageRegex = new Regex (
30
+ "--language (?<language>\\ w+)" ,
31
+ RegexOptions . Compiled | RegexOptions . ExplicitCapture | RegexOptions . Singleline ,
32
+ TimeSpan . FromSeconds ( 1 ) ) ;
33
+
34
+ public BaselineTest ( ProjectFactoryFixture projectFactory , ITestOutputHelper output )
35
+ {
36
+ ProjectFactory = projectFactory ;
37
+ Output = output ;
38
+ }
39
+
40
+ public Project Project { get ; set ; }
41
+
42
+ public static TheoryData < string , string [ ] > TemplateBaselines
43
+ {
44
+ get
45
+ {
46
+ using ( var stream = typeof ( BaselineTest ) . Assembly . GetManifestResourceStream ( "ProjectTemplates.Tests.template-baselines.json" ) )
47
+ {
48
+ using ( var jsonReader = new JsonTextReader ( new StreamReader ( stream ) ) )
49
+ {
50
+ var baseline = JObject . Load ( jsonReader ) ;
51
+ var data = new TheoryData < string , string [ ] > ( ) ;
52
+ foreach ( var template in baseline )
53
+ {
54
+ foreach ( var authOption in ( JObject ) template . Value )
55
+ {
56
+ data . Add (
57
+ ( string ) authOption . Value [ "Arguments" ] ,
58
+ ( ( JArray ) authOption . Value [ "Files" ] ) . Select ( s => ( string ) s ) . ToArray ( ) ) ;
59
+ }
60
+ }
61
+
62
+ return data ;
63
+ }
64
+ }
65
+ }
66
+ }
67
+
68
+ public ProjectFactoryFixture ProjectFactory { get ; }
69
+ public ITestOutputHelper Output { get ; }
70
+
71
+ [ Theory ]
72
+ [ MemberData ( nameof ( TemplateBaselines ) ) ]
73
+ public async Task Template_Produces_The_Right_Set_Of_FilesAsync ( string arguments , string [ ] expectedFiles )
74
+ {
75
+ Project = await ProjectFactory . GetOrCreateProject ( "baseline" + SanitizeArgs ( arguments ) , Output ) ;
76
+ var createResult = await Project . RunDotNetNewRawAsync ( arguments ) ;
77
+ Assert . True ( createResult . ExitCode == 0 , createResult . GetFormattedOutput ( ) ) ;
78
+
79
+ foreach ( var file in expectedFiles )
80
+ {
81
+ AssertFileExists ( Project . TemplateOutputDir , file , shouldExist : true ) ;
82
+ }
83
+
84
+ var filesInFolder = Directory . EnumerateFiles ( Project . TemplateOutputDir , "*" , SearchOption . AllDirectories ) ;
85
+ foreach ( var file in filesInFolder )
86
+ {
87
+ var relativePath = file . Replace ( Project . TemplateOutputDir , "" ) . Replace ( "\\ " , "/" ) . Trim ( '/' ) ;
88
+ if ( relativePath . EndsWith ( ".csproj" , StringComparison . Ordinal ) ||
89
+ relativePath . EndsWith ( ".fsproj" , StringComparison . Ordinal ) ||
90
+ relativePath . EndsWith ( ".props" , StringComparison . Ordinal ) ||
91
+ relativePath . EndsWith ( ".targets" , StringComparison . Ordinal ) ||
92
+ relativePath . StartsWith ( "bin/" , StringComparison . Ordinal ) ||
93
+ relativePath . StartsWith ( "obj/" , StringComparison . Ordinal ) )
94
+ {
95
+ continue ;
96
+ }
97
+ Assert . Contains ( relativePath , expectedFiles ) ;
98
+ }
99
+ }
100
+
101
+ private string SanitizeArgs ( string arguments )
102
+ {
103
+ var text = TemplateNameRegex . Match ( arguments )
104
+ . Groups . TryGetValue ( "template" , out var template ) ? template . Value : "" ;
105
+
106
+ text += AuthenticationOptionRegex . Match ( arguments )
107
+ . Groups . TryGetValue ( "auth" , out var auth ) ? auth . Value : "" ;
108
+
109
+ text += arguments . Contains ( "--uld" ) ? "uld" : "" ;
110
+
111
+ text += LanguageRegex . Match ( arguments )
112
+ . Groups . TryGetValue ( "language" , out var language ) ? language . Value . Replace ( "#" , "Sharp" ) : "" ;
113
+
114
+ if ( arguments . Contains ( "--support-pages-and-views true" ) )
115
+ {
116
+ text += "supportpagesandviewstrue" ;
117
+ }
118
+
119
+ return text ;
120
+ }
121
+
122
+ private void AssertFileExists ( string basePath , string path , bool shouldExist )
123
+ {
124
+ var fullPath = Path . Combine ( basePath , path ) ;
125
+ var doesExist = File . Exists ( fullPath ) ;
126
+
127
+ if ( shouldExist )
128
+ {
129
+ Assert . True ( doesExist , "Expected file to exist, but it doesn't: " + path ) ;
130
+ }
131
+ else
132
+ {
133
+ Assert . False ( doesExist , "Expected file not to exist, but it does: " + path ) ;
134
+ }
135
+ }
136
+ }
137
+ }
0 commit comments