Class: Brut::CLI::Apps::Scaffold::CustomElementTest

Inherits:
Commands::BaseCommand show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from Commands::BaseCommand

#accepts, #argv, #bootstrap?, #commands, #default_command, #default_command_class, #default_rack_env, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stderr, #stdin, #stdout, #system!

Instance Method Details

#args_descriptionObject



559
# File 'lib/brut/cli/apps/scaffold.rb', line 559

def args_description = "path_to_js_files..."

#descriptionObject



558
# File 'lib/brut/cli/apps/scaffold.rb', line 558

def description = "Create a test for a custom element in your app"

#runObject



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/brut/cli/apps/scaffold.rb', line 560

def run
  if argv.empty?
    stderr.puts "'custom-element-test' requires one or more files to scaffold a test for"
    return 1
  end

  if argv.any? { |file| Pathname(file).extname != ".js" }
    stderr.puts "'custom-element-test' must be given only .js files"
    return 1
  end

  files_to_create = argv.map { |arg|
    path = Pathname(arg).expand_path
    relative_path = path.relative_path_from(Brut.container.js_src_dir)
    relative_path_as_spec = relative_path.dirname / (relative_path.basename(relative_path.extname).to_s + ".spec.js")
    spec_path = Brut.container.js_specs_dir / relative_path_as_spec
    [ path, spec_path ]
  }

  existing_files = files_to_create.select { |_,spec|
    spec.exist?
  }

  if existing_files.any? && !global_options.overwrite?
    relative_paths = existing_files.map { |_,pathname| pathname.relative_path_from(Brut.container.project_root) }
    stderr.puts "Some files to be generated already exist. Set global option --overwrite to overwrite them:"
    relative_paths.each do |file|
      stderr.puts file
    end
    return 1
  end

  files_to_create.each do |source_file, spec_file|
    source_class = source_file.basename(source_file.extname)
    tag_name = File.read(source_file).split(/\n/).map { |line|
      if line =~ /static\s+tagName\s*=\s*\"([^"]+)\"/
        "<#{$1}>"
      else
        nil
      end
    }.compact.first
    description = tag_name || source_class
    code =  %{import { withHTML } from "brut-js/testing/index.js"

describe("#{description}", () => {
  withHTML(`
  #{ tag_name ? "#{tag_name}" : "<!-- HTML here -->" }
  #{ tag_name ? "#{tag_name.gsub(/^</,'</')}" : "" }
  `).test("description here", ({document,window,assert}) => {
assert.fail("test goes here")
  })
})}
    if global_options.dry_run?
      stdout.puts "Would generate this code:\n\n#{code}"
    else
      File.open(spec_file, "w") do |file|
        file.puts code
      end
    end
  end

  0
end