<commit_before>/*
 * sbt
 * Copyright 2011 - 2017, Lightbend, Inc.
 * Copyright 2008 - 2010, <NAME>
 * Licensed under BSD-3-Clause license (see LICENSE)
 */

package sbt.internal.util
package complete

import java.io.File
import sbt.io.IO

class FileExamplesTest extends UnitSpec {

  "listing all files in an absolute base directory" should
    "produce the entire base directory's contents" in {
    withDirectoryStructure() { ds =>
      ds.fileExamples().toList should contain theSameElementsAs (ds.allRelativizedPaths)
    }
  }

  "listing files with a prefix that matches none" should "produce an empty list" in {
    withDirectoryStructure(withCompletionPrefix = "z") { ds =>
      ds.fileExamples().toList shouldBe empty
    }
  }

  "listing single-character prefixed files" should "produce matching paths only" in {
    withDirectoryStructure(withCompletionPrefix = "f") { ds =>
      ds.fileExamples().toList should contain theSameElementsAs (ds.prefixedPathsOnly)
    }
  }

  "listing directory-prefixed files" should "produce matching paths only" in {
    withDirectoryStructure(withCompletionPrefix = "far") { ds =>
      ds.fileExamples().toList should contain theSameElementsAs (ds.prefixedPathsOnly)
    }
  }

  it should "produce sub-dir contents only when appending a file separator to the directory" in {
    withDirectoryStructure(withCompletionPrefix = "far" + File.separator) { ds =>
      ds.fileExamples().toList should contain theSameElementsAs (ds.prefixedPathsOnly)
    }
  }

  "listing files with a sub-path prefix" should "produce matching paths only" in {
    withDirectoryStructure(withCompletionPrefix = "far" + File.separator + "ba") { ds =>
      ds.fileExamples().toList should contain theSameElementsAs (ds.prefixedPathsOnly)
    }
  }

  "completing a full path" should "produce a list with an empty string" in {
    withDirectoryStructure(withCompletionPrefix = "bazaar") { ds =>
      ds.fileExamples().toList shouldEqual List("")
    }
  }

  def withDirectoryStructure[A](withCompletionPrefix: String = "")(
      thunk: DirectoryStructure => A): Unit = {
    IO.withTemporaryDirectory { tempDir =>
      val ds = new DirectoryStructure(withCompletionPrefix)
      ds.createSampleDirStructure(tempDir)
      ds.fileExamples = new FileExamples(ds.baseDir, withCompletionPrefix)
      thunk(ds)
    }
  }

  final class DirectoryStructure(withCompletionPrefix: String) {
    var fileExamples: FileExamples = _
    var baseDir: File = _
    var childFiles: List[File] = _
    var childDirectories: List[File] = _
    var nestedFiles: List[File] = _
    var nestedDirectories: List[File] = _

    def allRelativizedPaths: List[String] =
      (childFiles ++ childDirectories ++ nestedFiles ++ nestedDirectories)
        .map(IO.relativize(baseDir, _).get)

    def prefixedPathsOnly: List[String] =<commit_msg>Fix a "discarded non-Unit value" warning in FileExamplesTest<commit_after>/*
 * sbt
 * Copyright 2011 - 2017, Lightbend, Inc.
 * Copyright 2008 - 2010, <NAME>
 * Licensed under BSD-3-Clause license (see LICENSE)
 */

package sbt.internal.util
package complete

import java.io.File
import org.scalatest.Assertion
import sbt.io.IO

class FileExamplesTest extends UnitSpec {

  "listing all files in an absolute base directory" should
    "produce the entire base directory's contents" in {
    withDirectoryStructure() { ds =>
      ds.fileExamples().toList should contain theSameElementsAs (ds.allRelativizedPaths)
    }
  }

  "listing files with a prefix that matches none" should "produce an empty list" in {
    withDirectoryStructure(withCompletionPrefix = "z") { ds =>
      ds.fileExamples().toList shouldBe empty
    }
  }

  "listing single-character prefixed files" should "produce matching paths only" in {
    withDirectoryStructure(withCompletionPrefix = "f") { ds =>
      ds.fileExamples().toList should contain theSameElementsAs (ds.prefixedPathsOnly)
    }
  }

  "listing directory-prefixed files" should "produce matching paths only" in {
    withDirectoryStructure(withCompletionPrefix = "far") { ds =>
      ds.fileExamples().toList should contain theSameElementsAs (ds.prefixedPathsOnly)
    }
  }

  it should "produce sub-dir contents only when appending a file separator to the directory" in {
    withDirectoryStructure(withCompletionPrefix = "far" + File.separator) { ds =>
      ds.fileExamples().toList should contain theSameElementsAs (ds.prefixedPathsOnly)
    }
  }

  "listing files with a sub-path prefix" should "produce matching paths only" in {
    withDirectoryStructure(withCompletionPrefix = "far" + File.separator + "ba") { ds =>
      ds.fileExamples().toList should contain theSameElementsAs (ds.prefixedPathsOnly)
    }
  }

  "completing a full path" should "produce a list with an empty string" in {
    withDirectoryStructure(withCompletionPrefix = "bazaar") { ds =>
      ds.fileExamples().toList shouldEqual List("")
    }
  }

  def withDirectoryStructure[A](withCompletionPrefix: String = "")(
      thunk: DirectoryStructure => Assertion
  ): Assertion = {
    IO.withTemporaryDirectory { tempDir =>
      val ds = new DirectoryStructure(withCompletionPrefix)
      ds.createSampleDirStructure(tempDir)
      ds.fileExamples = new FileExamples(ds.baseDir, withCompletionPrefix)
      thunk(ds)
    }
  }

  final class DirectoryStructure(withCompletionPrefix: String) {
    var fileExamples: FileExamples = _
    var baseDir: File = _
    var childFiles: List[File] = _
    var childDirectories: List[File] = _
    var nestedFiles: List[File] = _
    var nestedDirectories: List[File] = _

    def allRelativizedPaths: List[String] =
      (childFiles ++ childDirectories ++ nestedFiles ++ nestedDirectories)
        .map(IO.relativize(baseDir, _).get)

    def prefixedPathsOnly: List[String] =
{'fraction_non_alphanumeric': 0.0950327699206623, 'fraction_numerical': 0.005864091065884788, 'mean_word_length': 3.85678391959799, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1117943', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1624, 'n_words': 459}