We need to provide a way to create a directory where we will put our copies. Let's open a second browser and examine the FileDirectory class. There's a #createDirectory: method.
"Create a directory with the given name in this directory. Fail if the
name is bad or if a file or directory with that name already exists."
primCreateDirectory: (self fullNameFor: localFileName)
We're going to need the ability to detect if a directory exists, and the ability to delete one. There are #directoryExists: and #deleteDirectory: methods in the FileDirectory class.
"Answer true if a directory of the given name exists. The given name
may be either a full path name or a local directory within this
"FileDirectory default directoryExists: FileDirectory default pathName"
| fName dir |
to: [:filePath :name |
fName _ name.
ifTrue: [dir := self]
ifFalse: [dir := FileDirectory on: filePath]].
ifTrue: [^ dir directoryNames includes: fName]
ifFalse: [^ dir directoryNames
anySatisfy: [:name | name sameAs: fName]]
"Delete the directory with the given name in this directory. Fail if the
path is bad or if a directory by that name does not exist."
primDeleteDirectory: (self fullNameFor: localDirName)
We have enough information to write a new "Private" method for our FileListTest. Create the following method:
| time dname |
time := Time now.
dname := time hhmm24 , time seconds printString.
(aDirectory directoryExists: dname)
ifTrue: [aDirectory deleteDirectory: dname].
aDirectory createDirectory: dname.
We have enough to write our test. Create the following method in the "test" category on our FileListTest class:
Note: Squeak will complain that it doesn't find a matching #copySelectedFileToDirectory: method anywhere when you try to save. That is correct, we have not written it yet. Tell Squeak to accept the undefined method name when this happens.
| dir dname subDir fname entry index listMorph originalFileStream
copiedFileStream originalContents copiedContents gui |
ifFalse: [^ nil].
dir := FileDirectory default.
dname := self createTempDirectoryIn: dir.
subDir := dir directoryNamed: dname.
fname := self createTempFileIn: dir.
gui := FileList openAsMorph.
"Since we just opened up the FileList we can assume it's directory is
entry := gui model fileList
detect: [:each | each includesSubString: fname]
index := gui model fileList indexOf: entry.
listMorph := self fileListMorph: gui.
listMorph selectionIndex: index.
gui model fileListIndex: index.
"Now that we faked the selection of our test file, ask our FileList to copy
the file to our new sub-directory."
gui model copySelectedFileToDirectory: subDir.
"See if it's there."
self assert: (subDir fileExists: fname).
"How about it's contents?"
originalFileStream := dir readOnlyFileNamed: fname.
copiedFileStream := subDir readOnlyFileNamed: fname.
originalContents := originalFileStream contentsOfEntireFile.
copiedContents := copiedFileStream contentsOfEntireFile.
self assert: originalContents = copiedContents.
"Done with the files and directory."
dir deleteFileNamed: fname.
subDir deleteFileNamed: fname.
dir deleteDirectory: dname.
Continue on to the next page of second FileList enhancement.
Back to the beginning of this example.fish mountain