2016-10-27 18:46:43 -07:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Runtime.CompilerServices ;
namespace Microsoft.DotNet.TestFramework
{
public class TestAssetInfo
{
2023-09-29 12:53:43 -07:00
// This is needed each release after we upgrade to 9.0 but the templates haven't been upgraded yet
public static readonly string currentTfm = "net9.0" ;
2016-10-27 18:46:43 -07:00
private readonly string [ ] FilesToExclude = { ".DS_Store" , ".noautobuild" } ;
2017-02-10 10:54:05 -08:00
public string AssetName { get ; private set ; }
2017-02-10 09:49:21 -08:00
2017-08-29 17:59:34 -07:00
public FileInfo DotnetExeFile = > _testAssets . DotnetCsprojExe ;
2016-10-27 18:46:43 -07:00
2017-08-29 17:59:34 -07:00
public string ProjectFilePattern = > "*.csproj" ;
2017-01-06 01:40:26 -08:00
2017-02-10 10:54:05 -08:00
public DirectoryInfo Root { get ; private set ; }
2016-10-27 18:46:43 -07:00
2017-08-29 17:59:34 -07:00
private TestAssets _testAssets { get ; }
internal TestAssetInfo ( DirectoryInfo root , string assetName , TestAssets testAssets )
2016-10-27 18:46:43 -07:00
{
2017-01-06 01:40:26 -08:00
if ( root = = null )
2016-10-27 18:46:43 -07:00
{
2017-01-06 01:40:26 -08:00
throw new ArgumentNullException ( nameof ( root ) ) ;
2016-10-27 18:46:43 -07:00
}
2017-01-06 01:40:26 -08:00
if ( string . IsNullOrWhiteSpace ( assetName ) )
{
throw new ArgumentException ( "Argument cannot be null or whitespace" , nameof ( assetName ) ) ;
}
2017-08-29 17:59:34 -07:00
if ( testAssets = = null )
2017-01-06 01:40:26 -08:00
{
2017-08-29 17:59:34 -07:00
throw new ArgumentNullException ( nameof ( testAssets ) ) ;
2017-01-06 01:40:26 -08:00
}
2016-10-27 18:46:43 -07:00
2017-02-10 10:54:05 -08:00
Root = root ;
2016-10-27 18:46:43 -07:00
2017-02-10 10:54:05 -08:00
AssetName = assetName ;
2017-01-06 01:40:26 -08:00
2017-08-29 17:59:34 -07:00
_testAssets = testAssets ;
2016-10-27 18:46:43 -07:00
}
public TestAssetInstance CreateInstance ( [ CallerMemberName ] string callingMethod = "" , string identifier = "" )
{
2017-01-06 01:40:26 -08:00
var instancePath = GetTestDestinationDirectory ( callingMethod , identifier ) ;
2016-10-27 18:46:43 -07:00
2017-01-06 01:40:26 -08:00
var testInstance = new TestAssetInstance ( this , instancePath ) ;
2016-10-27 18:46:43 -07:00
return testInstance ;
}
2017-01-06 01:40:26 -08:00
internal IEnumerable < FileInfo > GetSourceFiles ( )
{
ThrowIfTestAssetDoesNotExist ( ) ;
2017-02-10 10:54:05 -08:00
return Root . GetFiles ( "*.*" , SearchOption . AllDirectories )
. Where ( f = > ! FilesToExclude . Contains ( f . Name ) ) ;
2017-01-06 01:40:26 -08:00
}
private DirectoryInfo GetTestDestinationDirectory ( string callingMethod , string identifier )
2016-10-27 18:46:43 -07:00
{
2017-08-29 17:59:34 -07:00
return _testAssets . CreateTestDirectory ( AssetName , callingMethod , identifier ) ;
2016-10-27 18:46:43 -07:00
}
2017-01-06 01:40:26 -08:00
private void ThrowIfTestAssetDoesNotExist ( )
{
2017-02-10 10:54:05 -08:00
if ( ! Root . Exists )
2017-10-11 17:17:28 -07:00
{
throw new DirectoryNotFoundException ( $"Directory not found at '{Root.FullName}'" ) ;
}
2017-01-06 01:40:26 -08:00
}
2016-10-27 18:46:43 -07:00
}
}