11using NUnit . Framework ;
22using System . Collections ;
33using System . IO ;
4+ using System . Linq ;
45using UnityEditor . Graphing . Util ;
56using UnityEditor . ShaderGraph . Serialization ;
67using UnityEngine ;
@@ -10,63 +11,116 @@ namespace UnityEditor.ShaderGraph.UnitTests
1011 [ TestFixture ]
1112 class ImportUpdateTests
1213 {
13- public class ImportCases : IEnumerable
14+ public class SingleImportCases : IEnumerable
1415 {
1516 private const string kGraphsLocation = "PreviousGraphVersions/" ;
1617 public IEnumerator GetEnumerator ( )
1718 {
18- return Directory . GetFiles ( Application . dataPath + "/../" + kGraphsLocation , "*" , SearchOption . AllDirectories ) . GetEnumerator ( ) ;
19+ var versionDirs = Directory . GetDirectories ( Application . dataPath + "/../" + kGraphsLocation , "*" , SearchOption . TopDirectoryOnly ) ;
20+ foreach ( var dir in versionDirs )
21+ foreach ( var assetPath in Directory . GetFiles ( dir , "*" , SearchOption . TopDirectoryOnly ) )
22+ yield return assetPath ;
23+ }
24+ }
25+
26+ public class MultiImportDirectories : IEnumerable
27+ {
28+ private const string kGraphsLocation = "PreviousGraphVersions/" ;
29+ public IEnumerator GetEnumerator ( )
30+ {
31+ var versionDirs = Directory . GetDirectories ( Application . dataPath + "/../" + kGraphsLocation , "*" , SearchOption . TopDirectoryOnly ) ;
32+ foreach ( var dir in versionDirs )
33+ foreach ( var multiDir in Directory . GetDirectories ( dir , "*" , SearchOption . TopDirectoryOnly ) )
34+ yield return multiDir ;
1935 }
2036 }
2137
2238 [ OneTimeSetUp ]
2339 public void Setup ( )
2440 {
25- if ( ! AssetDatabase . IsValidFolder ( "Assets/Testing/ImportTests" ) )
41+ if ( ! Directory . Exists ( Application . dataPath + "Testing/ImportTests" ) )
42+ Directory . CreateDirectory ( Application . dataPath + "Testing/ImportTests" ) ;
43+ AssetDatabase . Refresh ( ) ;
44+
45+ if ( ! AssetDatabase . IsValidFolder ( "Assets/Testing/ImportTests" ) )
2646 {
2747 AssetDatabase . CreateFolder ( "Assets/Testing" , "ImportTests" ) ;
2848 }
2949 }
3050
31- [ TestCaseSource ( typeof ( ImportCases ) ) ]
32- public void CopyOverAndImport ( string assetPath )
51+ [ TestCaseSource ( typeof ( SingleImportCases ) ) ]
52+ public void ImportSingle ( string assetPath )
3353 {
3454 string fileName = Path . GetFileName ( assetPath ) ;
35- string fileNameNoExtension = Path . GetFileNameWithoutExtension ( assetPath ) ;
3655 string fileContents = File . ReadAllText ( assetPath ) ;
37- string fileExtension = Path . GetExtension ( assetPath ) . ToLower ( ) ;
38- bool isSubgraph = ( fileExtension == "shadersubgraph" ) ;
3956
40- string localFilePath = "Assets /Testing/ImportTests/" + fileName ;
41- string localFilePathNoExtension = "Assets/Testing/ImportTests/" + fileNameNoExtension ;
57+ string targetPath = Application . dataPath + " /Testing/ImportTests/" + fileName ;
58+ File . WriteAllText ( targetPath , fileContents ) ;
4259
43- File . WriteAllText ( Application . dataPath + "/Testing/ImportTests/" + fileName , fileContents ) ;
44- AssetDatabase . ImportAsset ( localFilePath , ImportAssetOptions . ForceSynchronousImport | ImportAssetOptions . ForceUpdate | ImportAssetOptions . DontDownloadFromCacheServer ) ;
45- var graphGuid = AssetDatabase . AssetPathToGUID ( localFilePath ) ;
60+ string unityLocalPath = "Assets/Testing/ImportTests/" + fileName ;
61+ TestImportAsset ( unityLocalPath , targetPath ) ;
62+ }
63+
64+ [ TestCaseSource ( typeof ( MultiImportDirectories ) ) ]
65+ public void ImportMulti ( string directory )
66+ {
67+ string sourceDir = Path . GetFullPath ( directory ) . TrimEnd ( Path . DirectorySeparatorChar ) ;
68+ string dirName = Path . GetFileName ( sourceDir ) ;
69+
70+ string targetDir = Application . dataPath + "/Testing/ImportTests/" + dirName ;
71+ DirectoryCopy ( sourceDir , targetDir , true , true ) ;
72+
73+ foreach ( var assetFullPath in Directory . GetFiles ( targetDir , "*.shader*" , SearchOption . TopDirectoryOnly ) )
74+ {
75+ if ( ! assetFullPath . EndsWith ( ".meta" ) )
76+ {
77+ string relativeFilePath = assetFullPath . Substring ( targetDir . Length ) ;
78+ string unityLocalPath = "Assets/Testing/ImportTests/" + dirName + relativeFilePath ;
79+ TestImportAsset ( unityLocalPath , assetFullPath ) ;
80+ }
81+ }
82+ }
83+
84+ public void TestImportAsset ( string unityLocalPath , string fullPath )
85+ {
86+ unityLocalPath = unityLocalPath . Replace ( "\\ " , "/" ) ;
87+ Debug . Log ( "Testing file: " + unityLocalPath ) ;
88+
89+ // invoke an import
90+ AssetDatabase . ImportAsset ( unityLocalPath , ImportAssetOptions . ForceSynchronousImport | ImportAssetOptions . ForceUpdate | ImportAssetOptions . DontDownloadFromCacheServer ) ;
91+
92+ // double check we can load it up and validate it
93+ string fileContents = File . ReadAllText ( fullPath ) ;
94+ Assert . Greater ( fileContents . Length , 0 ) ;
95+
96+ var graphGuid = AssetDatabase . AssetPathToGUID ( unityLocalPath ) ;
4697 var messageManager = new MessageManager ( ) ;
4798 GraphData graphData = new GraphData ( ) { assetGuid = graphGuid , messageManager = messageManager } ;
4899 MultiJson . Deserialize ( graphData , fileContents ) ;
49100 graphData . OnEnable ( ) ;
50101 graphData . ValidateGraph ( ) ;
51102
103+ string fileExtension = Path . GetExtension ( fullPath ) . ToLower ( ) ;
104+ bool isSubgraph = ( fileExtension == "shadersubgraph" ) ;
52105 if ( isSubgraph )
53106 {
54107 // check that the SubGraphAsset is the same after versioning twice
55108 // this is important to ensure we're not importing subgraphs non-deterministically when they are out-of-date on disk
56- AssetDatabase . ImportAsset ( localFilePath , ImportAssetOptions . ForceSynchronousImport | ImportAssetOptions . ForceUpdate | ImportAssetOptions . DontDownloadFromCacheServer ) ;
57- var subGraph = AssetDatabase . LoadAssetAtPath < SubGraphAsset > ( localFilePath ) ;
109+ AssetDatabase . ImportAsset ( unityLocalPath , ImportAssetOptions . ForceSynchronousImport | ImportAssetOptions . ForceUpdate | ImportAssetOptions . DontDownloadFromCacheServer ) ;
110+ var subGraph = AssetDatabase . LoadAssetAtPath < SubGraphAsset > ( unityLocalPath ) ;
58111 var serialized = EditorJsonUtility . ToJson ( subGraph ) ;
59112
60- AssetDatabase . ImportAsset ( localFilePath , ImportAssetOptions . ForceSynchronousImport | ImportAssetOptions . ForceUpdate | ImportAssetOptions . DontDownloadFromCacheServer ) ;
61- var subGraph2 = AssetDatabase . LoadAssetAtPath < SubGraphAsset > ( localFilePath ) ;
113+ AssetDatabase . ImportAsset ( unityLocalPath , ImportAssetOptions . ForceSynchronousImport | ImportAssetOptions . ForceUpdate | ImportAssetOptions . DontDownloadFromCacheServer ) ;
114+ var subGraph2 = AssetDatabase . LoadAssetAtPath < SubGraphAsset > ( unityLocalPath ) ;
62115 var serialized2 = EditorJsonUtility . ToJson ( subGraph2 ) ;
63116
64- Assert . AreEqual ( serialized , serialized2 , $ "Importing the subgraph { localFilePath } twice resulted in different subgraph assets.") ;
117+ Assert . AreEqual ( serialized , serialized2 , $ "Importing the subgraph { unityLocalPath } twice resulted in different subgraph assets.") ;
65118 }
66119 else
67120 {
68121 // check that the generated shader is the same after versioning twice
69122 // this is important to ensure we're not importing shaders non-deterministically when they are out-of-date on disk
123+ string fileNameNoExtension = Path . GetFileNameWithoutExtension ( fullPath ) ;
70124 var generator = new Generator ( graphData , graphData . outputNode , GenerationMode . ForReals , fileNameNoExtension , null ) ;
71125 string shader = generator . generatedShader ;
72126
@@ -78,18 +132,55 @@ public void CopyOverAndImport(string assetPath)
78132 var generator2 = new Generator ( graphData2 , graphData2 . outputNode , GenerationMode . ForReals , fileNameNoExtension , null ) ;
79133 string shader2 = generator2 . generatedShader ;
80134
81- Assert . AreEqual ( shader , shader2 , $ "Importing the graph { localFilePath } twice resulted in different generated shaders.") ;
135+ Assert . AreEqual ( shader , shader2 , $ "Importing the graph { unityLocalPath } twice resulted in different generated shaders.") ;
82136 }
83137 }
84138
85139 [ OneTimeTearDown ]
86140 public void Cleanup ( )
87141 {
142+ AssetDatabase . Refresh ( ) ;
88143 foreach ( string assetGuid in AssetDatabase . FindAssets ( "*" , new string [ ] { "Assets/Testing/ImportTests" } ) )
89144 {
90145 Debug . Log ( AssetDatabase . GUIDToAssetPath ( assetGuid ) ) ;
91146 AssetDatabase . DeleteAsset ( AssetDatabase . GUIDToAssetPath ( assetGuid ) ) ;
92147 }
93148 }
149+
150+ private static void DirectoryCopy ( string sourceDirName , string destDirName , bool copySubDirs , bool overwriteFiles )
151+ {
152+ // Get the subdirectories for the specified directory.
153+ DirectoryInfo dir = new DirectoryInfo ( sourceDirName ) ;
154+
155+ if ( ! dir . Exists )
156+ {
157+ throw new DirectoryNotFoundException (
158+ "Source directory does not exist or could not be found: "
159+ + sourceDirName ) ;
160+ }
161+
162+ DirectoryInfo [ ] dirs = dir . GetDirectories ( ) ;
163+
164+ // If the destination directory doesn't exist, create it.
165+ Directory . CreateDirectory ( destDirName ) ;
166+
167+ // Get the files in the directory and copy them to the new location.
168+ FileInfo [ ] files = dir . GetFiles ( ) ;
169+ foreach ( FileInfo file in files )
170+ {
171+ string tempPath = Path . Combine ( destDirName , file . Name ) ;
172+ file . CopyTo ( tempPath , overwriteFiles ) ;
173+ }
174+
175+ // If copying subdirectories, copy them and their contents to new location.
176+ if ( copySubDirs )
177+ {
178+ foreach ( DirectoryInfo subdir in dirs )
179+ {
180+ string tempPath = Path . Combine ( destDirName , subdir . Name ) ;
181+ DirectoryCopy ( subdir . FullName , tempPath , copySubDirs , overwriteFiles ) ;
182+ }
183+ }
184+ }
94185 }
95186}
0 commit comments