--[[----------------------------------------------------------------------------

WFTestFileUtilsTest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

	-- WF.core.os
local KSIdGenerator = require 'KSIdGenerator'
local KSPathUtils = require 'KSPathUtils'
local WFFileUtils = require 'WFFileUtils'
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'
local WFTestFileUtils = require 'WFTestFileUtils'

--------------------------------------------------------------------------------

if WFPlatformInfo.isAndroid then return {} end
	-- TODO [kcarlson 2016-01-01]: WFTestFileUtils will clean up directories
	-- on Android but because WFFolderWalker is not implemented it is unable
	-- to detect leaked files.  Probably not real important as we will catch
	-- them on the other platforms.

if WFPlatformInfo.isWindows then return {} end
	-- FIXME [jandrew 2018-03-22]: Disabling this test suite it's unreliable
	-- on Windows.
	
--============================================================================--

local WFTestFileUtilsTest = {}

--------------------------------------------------------------------------------

local child = KSPathUtils.child

local testDataPath = child( KSPathUtils.getStandardFilePath( 'testdata' ), 'wffileutils' )

--------------------------------------------------------------------------------

function WFTestFileUtilsTest.setUpClass()

	WFTestFileUtils.checkForUndeletedFiles()
		-- make sure there aren't any stragglers before we start the test

end

--------------------------------------------------------------------------------

function WFTestFileUtilsTest.rxTestDetectFileLeftInCarouselDocuments( rxTest )

	rxTest:setTimeLimit( 60 )

	local docFolder = KSPathUtils.getStandardFilePath( 'carouselDocuments' )

	local dstFile = WFFileUtils.copyToUniquelyNamedFile(
		child( testDataPath, 'file.txt' ),
		child( docFolder, 'file.txt' ) )

	rxTest:expectEqual( 'string', type( dstFile ) )

	rxTest:finally( function()
		WFFileUtils.delete( dstFile )
			-- in case test fails
	end )

	rxTest:expectEqual( dstFile, WFTestFileUtils.checkForUndeletedFiles() )

	rxTest:expectEqual( nil, WFTestFileUtils.checkForUndeletedFiles() )
		-- Second check should find nothing, unwanted files were moved to the trash.

end


--------------------------------------------------------------------------------

function WFTestFileUtilsTest.rxTestDetectFileLeftInCarouselDocumentsSubFolder( rxTest )

	rxTest:setTimeLimit( 60 )

	local docFolder = KSPathUtils.getStandardFilePath( 'carouselDocuments' )

	local docSubFolder = child( docFolder, rxTest:testName() )

	WFFileUtils.mkdir( docSubFolder )

	rxTest:finally( function()
		WFFileUtils.delete( docSubFolder )
	end )

	local dstFile = WFFileUtils.copyToUniquelyNamedFile(
		child( testDataPath, 'file.txt' ),
		child( docSubFolder, 'file.txt' ) )

	rxTest:expectEqual( 'string', type( dstFile ) )

	rxTest:expectEqual( dstFile, WFTestFileUtils.checkForUndeletedFiles() )

	rxTest:expectEqual( nil, WFTestFileUtils.checkForUndeletedFiles() )
		-- Second check should find nothing, unwanted files were moved to the trash.

end

--------------------------------------------------------------------------------

function WFTestFileUtilsTest.rxTestDetectFileLeftInTempFolder( rxTest )

	rxTest:setTimeLimit( 60 )

	-- Detect a file left in a dynamically generated path in a temp directory.

	local tempSubFolder = child( KSPathUtils.getStandardFilePath( 'temp' ),
		rxTest:testName() .. KSIdGenerator.uuidInLowercaseHex() )

	WFFileUtils.mkdir( tempSubFolder )

	rxTest:finally( function()
		WFFileUtils.delete( tempSubFolder )
	end )

	WFTestFileUtils.addTestFolder(tempSubFolder)
		-- Must add dynamically created folders if you want to check them
		-- for garbage left behind.

	local dstFile = WFFileUtils.copyToUniquelyNamedFile(
		child( testDataPath, 'file.txt' ),
		child( tempSubFolder, rxTest:testName() ) )

	rxTest:expectEqual( 'string', type( dstFile ) )

	rxTest:expectEqual( KSPathUtils.leafName( dstFile ), KSPathUtils.leafName( WFTestFileUtils.checkForUndeletedFiles() ) )
		-- compare leaf names as mac may add a /private in front of the path when iterating a directory

	rxTest:expectEqual( nil, WFTestFileUtils.checkForUndeletedFiles() )
		-- Second check should find nothing, unwanted files were moved to the trash.

end
--------------------------------------------------------------------------------

function WFTestFileUtilsTest.rxTestIgnoresOzUser( rxTest )

	rxTest:setTimeLimit( 60 )

	local docFolder = KSPathUtils.getStandardFilePath( 'carouselDocuments' )

	local dstFile = WFFileUtils.copyToUniquelyNamedFile(
		child( testDataPath, 'file.txt' ),
		child( docFolder, 'oz-user-0a9439e20cd99b256f5f64570e2cab82' ) )

	rxTest:expectEqual( 'string', type( dstFile ) )

	rxTest:expectEqual( nil, WFTestFileUtils.checkForUndeletedFiles() )

end

--------------------------------------------------------------------------------

return WFRxTestCase.wrapSuite( WFTestFileUtilsTest )
