UnitTests/PathResolveTests.cs

Unit tests for PathResolve helper functions. Creates temporary directories and files to verify detection of a Garry's Mod root, path normalization and sanitization, material name remapping, making asset paths relative, and counting material/source files in folders.

File Access
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GmodAssetImporter;

[TestClass]
public class PathResolveTests
{
	[TestMethod]
	public void DetectsGmodRootFromNestedModelPath()
	{
		var root = Path.Combine( Path.GetTempPath(), "gmodassetimporter_gmod_" + Guid.NewGuid().ToString( "N" ) );
		var models = Path.Combine( root, "models", "weapons" );
		Directory.CreateDirectory( models );
		Directory.CreateDirectory( Path.Combine( root, "materials" ) );
		var mdl = Path.Combine( models, "gun.mdl" );
		File.WriteAllText( mdl, "x" );

		try
		{
			var detected = PathResolve.DetectGmodRoot( mdl );
			Assert.AreEqual( Path.GetFullPath( root ), Path.GetFullPath( detected ) );

			var rel = PathResolve.GetModelsRelativeDirectory( mdl, root );
			Assert.AreEqual( "models/weapons", rel.Replace( '\\', '/' ) );
		}
		finally
		{
			try { Directory.Delete( root, true ); } catch { /* ignore */ }
		}
	}

	[TestMethod]
	public void StripsMeshExtensionsAndMakesAssetsRelative()
	{
		Assert.AreEqual( "studio_w_m9", PathResolve.StripMeshSourceExtension( "studio_w_m9.smd" ) );
		Assert.AreEqual( "studio_w_m9", PathResolve.SanitizeAssetName( "studio_w_m9.smd.smd" ) );
		Assert.AreEqual( "weapon_m9_dm.vmat", PathResolve.MaterialRemapFromName( "materials/foo/weapon_m9_dm.vmt" ) );
		Assert.AreEqual( "weapon_m9_dm", PathResolve.MaterialSlotName( "weapon_m9_dm.vmat" ) );
		Assert.AreEqual( "gun.body", PathResolve.MaterialSlotName( "models/weapons/gun.body" ) );
		Assert.AreEqual( "gun.mag.vmat", PathResolve.MaterialRemapFromName( "gun.mag" ) );

		var abs = @"F:\game\Assets\test\models\weapons\gun.fbx";
		var rel = PathResolve.MakeAssetsRelative( abs, @"F:\game\Assets\test" );
		Assert.AreEqual( "test/models/weapons/gun.fbx", rel.Replace( '\\', '/' ) );
	}

	[TestMethod]
	public void CountsVtfVmtInFolder()
	{
		var root = Path.Combine( Path.GetTempPath(), "gmodassetimporter_scan_" + Guid.NewGuid().ToString( "N" ) );
		var mats = Path.Combine( root, "materials", "weapons" );
		Directory.CreateDirectory( mats );
		File.WriteAllText( Path.Combine( mats, "gun.vtf" ), "x" );
		File.WriteAllText( Path.Combine( mats, "gun.vmt" ), "x" );
		File.WriteAllText( Path.Combine( mats, "scope.vtf" ), "x" );

		try
		{
			var counts = PathResolve.CountSourceAssets( root, recursive: true );
			Assert.AreEqual( 2, counts.Vtf );
			Assert.AreEqual( 1, counts.Vmt );
			Assert.AreEqual( 0, counts.Mdl );

			var token = PathResolve.GetMaterialsRelativeToken( Path.Combine( mats, "gun.vtf" ), root );
			Assert.AreEqual( "weapons/gun", token.Replace( '\\', '/' ) );
		}
		finally
		{
			try { Directory.Delete( root, true ); } catch { /* ignore */ }
		}
	}
}