Second Enhancement to File List

continued


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.

createDirectory: localFileName
    "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."

    self
        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.

directoryExists: filenameOrPath
    "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
     directory. "
    "FileDirectory default directoryExists: FileDirectory default pathName"

    | fName dir |
    FileDirectory
        splitName: filenameOrPath
        to: [:filePath :name |
            fName _ name.
            filePath
isEmpty
                ifTrue: [dir := self]
                ifFalse: [dir := FileDirectory on: filePath]].
    self isCaseSensitive
        ifTrue: [^ dir directoryNames includes: fName]
        ifFalse: [^ dir directoryNames
                anySatisfy: [:name | name sameAs: fName]]


deleteDirectory: localDirName
    "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."

    self
        primDeleteDirectory: (self fullNameFor: localDirName)

We have enough information to write a new "Private" method for our FileListTest. Create the following method:

createTempDirectoryIn: aDirectory
    | time dname |
    time := Time now.
    dname := time hhmm24 , time seconds printString.
    (aDirectory directoryExists: dname)
        ifTrue: [aDirectory deleteDirectory: dname].
    aDirectory createDirectory: dname.
    ^ 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.
testFileLocalCopy
    | dir dname subDir fname entry index listMorph originalFileStream
      copiedFileStream originalContents copiedContents gui
|
    Smalltalk isMorphic
        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
    correct. "
    entry := gui model fileList
                detect: [:each | each includesSubString: fname]
                ifNone: [].
    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.
    originalFileStream close.
    copiedFileStream close.
    self assert: originalContents = copiedContents.
    "Done with the files and directory."
    dir deleteFileNamed: fname.
    subDir deleteFileNamed: fname.
    dir deleteDirectory: dname.
    gui delete

Continue on to the next page of second FileList enhancement.

Back to the beginning of this example.